@2fd/command 中文文档教程

发布于 8年前 浏览 31 项目主页 更新于 3年前

@2fd/command Build Status

模块化命令行界面

install

    npm install --save @2fd/command

Node Compatibility

  • node v6
  • node v5
  • node v4
  • node v0.12

Goals

  • 延迟加载。

    命令可以定义为表示其位置的字符串, 这允许生成复杂的工具而无需大量的 文件减慢启动速度。

  • 命令是用户定义的。

    • 调用命令的名称由实现定义, 这允许您指定更实用的命令 (例如 'migration:init' 或 'mgt:i' 而不是 'module-name:migracions:init')。

    • 第三方包只能提示命令列表和名称 实施它们,但您总是只能实施您需要的那些 甚至用你自己的替换它们。

  • 完全模块化。 一个简单且完全面向对象的 API 可以轻松地:

    • 创建可以与社区共享的命令

    • 重新实现和/或扩展任何功能

    • 集成第三方创建的命令

  • 包含定义

    如果您使用 Typescript 编程,定义包含在包中 因此,当您使用 npm 作为依赖安装而不使用时,它们是可用的 需要定义管理器作为 typingstsd

Usage

命令实现

    import {
        Command,
        CommandInterface,
        BooleanFlag,
        Param
    } from '@2fd/command';

    // Object command implementation
    class MyCommand extends Command implements CommandInterface {

        description = 'My Command description';

        params = new Param('requireParam [optionalParam] [...optionalParamList]');

        flags = [
            new BooleanFlag('force', ['--force', '-f'], 'Force flag'),
        ];

        // action(input, output) { }; in javascript
        action(input: InputInterface, output: OutputInterface): void {

            // input.params.requireParam: string
            // input.params.optionalParam?: string
            // input.params.optionalParamList: Array<string>
            // input.flags.force: boolean
        };
    }

    export let myCommand = new MyCommand;

    // Function command implementation
    export function myQuickCommand(input: InputInterface, output: OutputInterface): void {
        // Do something
    }

多命令执行器

    // cmd.js

    import {
        ExecutorCommand,
        ArgvInput,
        ConsoleOutput
    } from '@2fd/command';

    import {myCommand} from './path/to/commands';

    let tool = new ExecutorCommand();

    tool.version = '1.0.0';
    tool.description = 'Command description';

    tool.addCommand('run', myCommand );
    // or
    tool.addCommand('run', './path/to/commands#myCommand' );

    tool.addCommands({
        'command1' : './path/to/commands#myCommand',
        'command2' : './path/to/commands#myCommand'
    });

    tool.addCommadsNS('ns', {
        'command3' : './path/to/commands#myCommand',
        'command4' : './path/to/commands#myCommand'
    });

    tool.handle(
        new ArgvInput(process.argv),
        new ConsoleOutput()
    );
    > node cmd.js

        Command description [v1.0.0]

        Usage: node cmd.js [COMMAND]

        run            My Command description
        command1       My Command description
        command2       My Command description
        ns:command3    My Command description
        ns:command4    My Command description

文件中的简单命令

    // command.js

    import {myCommand} from './path/to/commands';

    myCommand.handle(
        new ArgvInput(process.argv),
        new ConsoleOutput()
    );
    > node command.js --help

        Usage: node command.js [OPTIONS] requireParam [optionalParam] [...optionalParamList]

        My Command description

        --force, -f    Force flag
        --help, -h     Print this help

@2fd/command Build Status

Modular command line interface

install

    npm install --save @2fd/command

Node Compatibility

  • node v6
  • node v5
  • node v4
  • node v0.12

Goals

  • Lazy load.

    Commands can be defined as a string that represents its location, which allows to generate complex tools without the large number of files slowing down the boot.

  • Commands are user-defined.

    • The name that invokes a command is defined by the implementation, which allows you to specify more practical commands (such as 'migration:init'or 'mgt:i' instead of 'module-name:migracions:init').

    • Third-party packages can only suggest the list of commands and names that implement them, but always you can implement only those you need and even replace them with your own ones.

  • Fully modular. A simple and completely object-oriented APIs allow easily:

    • Create commands that can be shared with the community

    • Re-implement and/or expand any functionality

    • Integrate commands created by third parties

  • Definitions included

    If you program in Typescript, definitions are included in the package so they are available when you install as dependincie with npm without requiring a definitions manager as typings or tsd

Usage

Command implementation

    import {
        Command,
        CommandInterface,
        BooleanFlag,
        Param
    } from '@2fd/command';

    // Object command implementation
    class MyCommand extends Command implements CommandInterface {

        description = 'My Command description';

        params = new Param('requireParam [optionalParam] [...optionalParamList]');

        flags = [
            new BooleanFlag('force', ['--force', '-f'], 'Force flag'),
        ];

        // action(input, output) { }; in javascript
        action(input: InputInterface, output: OutputInterface): void {

            // input.params.requireParam: string
            // input.params.optionalParam?: string
            // input.params.optionalParamList: Array<string>
            // input.flags.force: boolean
        };
    }

    export let myCommand = new MyCommand;

    // Function command implementation
    export function myQuickCommand(input: InputInterface, output: OutputInterface): void {
        // Do something
    }

Multiple command executor

    // cmd.js

    import {
        ExecutorCommand,
        ArgvInput,
        ConsoleOutput
    } from '@2fd/command';

    import {myCommand} from './path/to/commands';

    let tool = new ExecutorCommand();

    tool.version = '1.0.0';
    tool.description = 'Command description';

    tool.addCommand('run', myCommand );
    // or
    tool.addCommand('run', './path/to/commands#myCommand' );

    tool.addCommands({
        'command1' : './path/to/commands#myCommand',
        'command2' : './path/to/commands#myCommand'
    });

    tool.addCommadsNS('ns', {
        'command3' : './path/to/commands#myCommand',
        'command4' : './path/to/commands#myCommand'
    });

    tool.handle(
        new ArgvInput(process.argv),
        new ConsoleOutput()
    );
    > node cmd.js

        Command description [v1.0.0]

        Usage: node cmd.js [COMMAND]

        run            My Command description
        command1       My Command description
        command2       My Command description
        ns:command3    My Command description
        ns:command4    My Command description

Simple command in file

    // command.js

    import {myCommand} from './path/to/commands';

    myCommand.handle(
        new ArgvInput(process.argv),
        new ConsoleOutput()
    );
    > node command.js --help

        Usage: node command.js [OPTIONS] requireParam [optionalParam] [...optionalParamList]

        My Command description

        --force, -f    Force flag
        --help, -h     Print this help
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文