<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ### Nest Commander 擴展 \[獨立應用程序\](https://docs.nestjs.com/standalone-applications)docs 還有 \[nest-commander\](https://jmcdo29.github.io/nest-commander) 包用于編寫命令行應用程序的結構類似于典型的 Nest 應用程序。 > **INFO**`nest-commander`是第三方包,不由整個NestJS核心團隊管理。請報告在\[適當的存儲庫\](https://github.com/jmcdo29/nest-commander/issues/new/choose) 中發現的庫中的任何問題 #### 安裝 就像任何其他軟件包一樣,您必須先安裝它才能使用它。 ~~~bash $ npm i nest-commander ~~~ #### 命令文件 `nest-commander` 使得使用 \[decorators\](https://www.typescriptlang.org/docs/handbook/decorators.html) 通過類的`@Command()`裝飾器編寫新的命令行應用程序和該類的方法的`@Option()`裝飾器。每個命令文件都應該實現`CommandRunner`接口并且應該用`@Command()`裝飾器來裝飾。 Nest 將每個命令都視為一個`@Injectable()`,因此您的正常依賴注入仍然可以正常工作。唯一需要注意的是接口`CommandRunner`,應該由每個命令實現。 `CommandRunner`接口確保所有命令都有一個`run`方法,該方法返回一個`Promise`并接受參數`string\[\], Record`。 `run` 命令是您可以從其中啟動所有邏輯的地方,它將接收任何與選項標志不匹配的參數并將它們作為數組傳遞,以防萬一您真的打算使用多個參數。至于選項,`Record`,這些屬性的名稱與給定給`@Option()`裝飾器的`name`屬性匹配,而它們的值與選項處理程序的返回值匹配。如果您想要更好的類型安全性,也歡迎您為您的選項創建一個界面。 #### 運行命令 類似于在 NestJS 應用程序中我們可以使用`NestFactory` 為我們創建服務器,并使用`listen` 運行它,`nest-commander` 包公開了一個簡單易用的 API 來運行您的服務器。導入`CommandFactory`并使用`static`方法`run`并傳入應用程序的根模塊。這可能如下所示 ~~~ts import { CommandFactory } from 'nest-commander'; import { AppModule } from './app.module'; async function bootstrap() { await CommandFactory.run(AppModule); } bootstrap(); ~~~ 默認情況下,使用 CommandFactory 時 Nest 的記錄器是禁用的。不過可以提供它,作為`run`函數的第二個參數。您可以提供自定義的 NestJS 記錄器,或者您想要保留的日志級別數組 - 如果您只想打印 Nest 的錯誤日志,至少在此處提供`\['error'\]` 可能很有用。 ~~~ts import { CommandFactory } from 'nest-commander'; import { AppModule } from './app.module'; import { LogService } './log.service'; async function bootstrap() { await CommandFactory.run(AppModule, new LogService()); // or, if you only want to print Nest's warnings and errors await CommandFactory.run(AppModule, ['warn', 'error']); } bootstrap(); ~~~ 就是這樣。在底層,`CommandFactory` 會為你調用`NestFactory` 并在必要時調用`app.close()`,所以你不必擔心那里的內存泄漏。如果你需要添加一些錯誤處理,總是有`try/catch`包裝`run`命令,或者你可以將一些`.catch()`方法鏈接到`bootstrap()`調用。 #### 測試 那么如果你不能超級容易地測試它,那么編寫一個超級棒的命令行腳本有什么用,對吧?幸運的是,`nest-commander` 有一些實用程序,您可以使用它們與 NestJS 生態系統完美契合,任何 Nestlings 都會有賓至如歸的感覺。您可以使用“CommandTestFactory”并傳入元數據,而不是使用“CommandFactory”在測試模式下構建命令,這與來自“@nestjs/testing”的“Test.createTestingModule”的工作方式非常相似。事實上,它在后臺使用了這個包。你還可以在調用`compile()`之前鏈接`overrideProvider`方法,這樣你就可以在測試中換掉DI片段。 #### 一起使用 下面的類相當于有一個 CLI 命令,它可以接受子命令`basic` 或直接調用,支持`-n`、`-s` 和`-b`(以及它們的長標志)并為每個選項使用自定義解析器。也支持`--help`標志,這是指揮官的習慣。 ~~~ts import { Command, CommandRunner, Option } from 'nest-commander'; import { LogService } from './log.service'; interface BasicCommandOptions { string?: string; boolean?: boolean; number?: number; } @Command({ name: 'basic', description: 'A parameter parse' }) export class BasicCommand implements CommandRunner { constructor(private readonly logService: LogService) {} async run( passedParam: string[], options?: BasicCommandOptions, ): Promise<void> { if (options?.boolean !== undefined && options?.boolean !== null) { this.runWithBoolean(passedParam, options.boolean); } else if (options?.number) { this.runWithNumber(passedParam, options.number); } else if (options?.string) { this.runWithString(passedParam, options.string); } else { this.runWithNone(passedParam); } } @Option({ flags: '-n, --number [number]', description: 'A basic number parser', }) parseNumber(val: string): number { return Number(val); } @Option({ flags: '-s, --string [string]', description: 'A string return', }) parseString(val: string): string { return val; } @Option({ flags: '-b, --boolean [boolean]', description: 'A boolean parser', }) parseBoolean(val: string): boolean { return JSON.parse(val); } runWithString(param: string[], option: string): void { this.logService.log({ param, string: option }); } runWithNumber(param: string[], option: number): void { this.logService.log({ param, number: option }); } runWithBoolean(param: string[], option: boolean): void { this.logService.log({ param, boolean: option }); } runWithNone(param: string[]): void { this.logService.log({ param }); } } ~~~ 確保將命令類添加到模塊 ~~~ts @Module({ providers: [LogService, BasicCommand], }) export class AppModule {} ~~~ 現在為了能夠在 main.ts 中運行 CLI,您可以執行以下操作 ~~~ts async function bootstrap() { await CommandFactory.run(AppModule); } bootstrap(); ~~~ 就像這樣,你有一個命令行應用程序。 #### 更多信息 訪問 \[nest-commander 文檔站點\](https://jmcdo29.github.io/nest-commander) 了解更多信息、示例和 API 文檔。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看