Skip to content
This repository
Browse code

Add CommanderJs definitions

  • Loading branch information...
commit fce7051232f9ba37464933c125749e7d4875ae13 1 parent 921a6e6
Marcelo Dezem mdezem authored
3  README.md
Source Rendered
@@ -41,9 +41,10 @@ List of Definitions
41 41 * [Chosen](http://harvesthq.github.com/chosen/) (by [Boris Yankov](https://github.com/borisyankov))
42 42 * [Chrome](http://developer.chrome.com/extensions/) (by [Matthew Kimber](https://github.com/matthewkimber))
43 43 * [CodeMirror](http://codemirror.net) (by [Fran�ois de Campredon](https://github.com/fdecampredon))
  44 +* [Commander](http://github.com/visionmedia/commander.js) (by [Marcelo Dezem](https://github.com/mdezem))
44 45 * [d3.js](http://d3js.org/) (from TypeScript samples)
45 46 * [domo](http://domo-js.com/) (by [Steve Fenton](https://github.com/Steve-Fenton))
46   -* [dust](http://linkedin.github.com/dustjs) (by [Marcelo Dezem](https://github.com/mdezem)
  47 +* [dust](http://linkedin.github.com/dustjs) (by [Marcelo Dezem](https://github.com/mdezem))
47 48 * [EaselJS](http://www.createjs.com/#!/EaselJS) (by [Pedro Ferreira](https://bitbucket.org/drk4))
48 49 * [ember.js](http://emberjs.com/) (by [Boris Yankov](https://github.com/borisyankov))
49 50 * [EpicEditor](http://epiceditor.com/) (by [Boris Yankov](https://github.com/borisyankov))
50 commander/commander-tests.ts
... ... @@ -0,0 +1,50 @@
  1 +///<reference path="commander.d.ts"/>
  2 +
  3 +//
  4 +// TODO: improve tests
  5 +// [the code below was extracted from the documentation and examples, but does not seem to cover all cases]
  6 +//
  7 +
  8 +import program = module("commander");
  9 +
  10 +program
  11 + .version('0.0.1')
  12 + .option('-C, --chdir <path>', 'change the working directory')
  13 + .option('-c, --config <path>', 'set config path [./deploy.conf]')
  14 + .option('-T, --no-tests', 'ignore test hook')
  15 +
  16 +// $ deploy setup stage
  17 +// $ deploy setup
  18 +program
  19 + .command('setup [env]')
  20 + .description('run setup commands for all envs')
  21 + .action(function (env) {
  22 + env = env || 'all';
  23 + console.log('setup for %s env(s)', env);
  24 + });
  25 +
  26 +// $ deploy stage
  27 +// $ deploy production
  28 +program
  29 + .command('*')
  30 + .action(function (env) {
  31 + console.log('deploying "%s"', env);
  32 + });
  33 +
  34 +program.option('-p, --pepper', 'add pepper');
  35 +
  36 +program.option('-C, --chdir <path>', 'change the working directory');
  37 +
  38 +program.prompt('Username: ', function (name) {
  39 + console.log('hi %s', name);
  40 +});
  41 +
  42 +program.prompt('Description:', function (desc) {
  43 + console.log('description was "%s"', desc.trim());
  44 +});
  45 +
  46 +program.promptForNumber("Enter a number:", (n) => { });
  47 +
  48 +program.confirm("Confirm? ", (f) => { });
  49 +
  50 +program.choose(["a", "b", "c"], (i) => { });
228 commander/commander.d.ts
... ... @@ -0,0 +1,228 @@
  1 +// Type definitions for commanderjs 1.1.1
  2 +// Project: https://github.com/visionmedia/commander.js
  3 +// Definitions by: Marcelo Dezem <http://github.com/mdezem>
  4 +// Definitions: https://github.com/borisyankov/DefinitelyTyped
  5 +
  6 +declare module "commander" {
  7 + export interface Command {
  8 + /**
  9 + * The command name.
  10 + */
  11 + name: string;
  12 +
  13 + //
  14 + //
  15 + // NOTE: the methods below are COPIED to the module
  16 + // as functions exports. If changes need to be made here,
  17 + // remember to re-paste the definitions in the module.
  18 + // Read below to know why such ugly thing is required.
  19 + //
  20 + //
  21 +
  22 + /**
  23 + * Register callback fn for the command.
  24 + */
  25 + action(fn: (...args: any[]) => any): Command;
  26 +
  27 + /**
  28 + * Define option with flags, description and optional coercion function and default value.
  29 + * The flags string should contain both the short and long flags
  30 + * separated by comma, a pipe or space. The following are all valid
  31 + * all will output this way when --help is used.
  32 + *
  33 + * "-p, --pepper"
  34 + * "-p|--pepper"
  35 + * "-p --pepper"
  36 + *
  37 + * @param flags the option flags.
  38 + * @param description the option description. The description is printed when "--help" is used.
  39 + * @param coerceFn (optional) specifies a callback function to coerce the option arg.
  40 + * @param defaultValue (optional) specifies a default value.
  41 + */
  42 + option(flags: string, description: string, coerceFn?: (value: string) => any, defaultValue?: any): Command;
  43 +
  44 +
  45 + /**
  46 + * Sets the command version
  47 + */
  48 + version(version: string): Command;
  49 +
  50 + /**
  51 + * Parse the arguments array and invokes the commands passing the parsed options.
  52 + * @param argv the arguments array.
  53 + */
  54 + parse(argv: string[]): Command;
  55 +
  56 + /**
  57 + * Gets or sets the command description.
  58 + * @param description the new description for the command. When ommited this returns the current description, otherwise returns the current Command.
  59 + */
  60 + description(description: string): Command;
  61 + description(): string;
  62 +
  63 + /**
  64 + * Gets or sets the usage help string.
  65 + */
  66 + usage(usage: string): Command;
  67 + usage(): string;
  68 +
  69 + /*
  70 + * Prompt the user for a value, calling the callback function.
  71 + *
  72 + * Supports single-line and multi-line prompts.
  73 + * To issue a single-line prompt simply add a whitespace
  74 + * to the end of label, something like "name: ", whereas
  75 + * for a multi-line prompt omit this "description:".
  76 + * @param label the label string to be printed in console.
  77 + * @param callback a callback function to handle the inputed string.
  78 + */
  79 + prompt(label: string, callback: (value: string) => any): void;
  80 +
  81 + promptForNumber(label: string, callback: (value: number) => any): void;
  82 + promptForDate(label: string, callback: (value: Date) => any): void;
  83 + promptSingleLine(label: string, callback: (value: string) => any): void;
  84 + promptMultiLine(label: string, callback: (value: string) => any): void;
  85 +
  86 + /**
  87 + * Prompt for password with a label, a optional mask char and callback function.
  88 + * The mask string defaults to '', aka no output is written while typing, you may want to use "*" etc.
  89 + */
  90 + password(label: string, mask: string, callback: (value: string) => any): void;
  91 + password(label: string, callback: (value: string) => any): void;
  92 +
  93 + /**
  94 + * Prompts the user for a confirmation.
  95 + */
  96 + confirm(label: string, callback: (flag: bool) => any): void;
  97 +
  98 + /**
  99 + * Prompt for password with str, mask char and callback fn(val).
  100 + * The mask string defaults to '', aka no output is written while typing, you may want to use "*" etc.
  101 + */
  102 + choose(options: string[], callback: (idx: number) => any): void;
  103 + choose(options: any[], callback: (idx: number) => any): void;
  104 +
  105 + /**
  106 + * Add command with the specified name. Returns a new instance of Command.
  107 + *
  108 + * The .action() callback is invoked when the
  109 + * command name is specified via ARGV,
  110 + * and the remaining arguments are applied to the
  111 + * function for access.
  112 +
  113 + * When the name is "*" an un-matched command
  114 + * will be passed as the first arg, followed by
  115 + * the rest of ARGV remaining.
  116 + *
  117 + * @param name the name of the command. Pass "*" to trap un-matched commands.
  118 + */
  119 + command(name: string): Command;
  120 + }
  121 +
  122 + //
  123 + //
  124 + // since TypeScript (and ECMA6) does not supports module.exports,
  125 + // there is no way to set the default Command instance as the module itself.
  126 + // It's ugly but the only way is to copy all the methods from Command
  127 + // and paste it in the module as functions exports.
  128 + //
  129 + //
  130 +
  131 + /**
  132 + * Register callback fn for the command.
  133 + */
  134 + export function action(fn: (...args: any[]) => any): Command;
  135 +
  136 + /**
  137 + * Define option with flags, description and optional coercion function and default value.
  138 + * The flags string should contain both the short and long flags
  139 + * separated by comma, a pipe or space. The following are all valid
  140 + * all will output this way when --help is used.
  141 + *
  142 + * "-p, --pepper"
  143 + * "-p|--pepper"
  144 + * "-p --pepper"
  145 + *
  146 + * @param flags the option flags.
  147 + * @param description the option description. The description is printed when "--help" is used.
  148 + * @param coerceFn (optional) specifies a callback function to coerce the option arg.
  149 + * @param defaultValue (optional) specifies a default value.
  150 + */
  151 + export function option(flags: string, description: string, coerceFn?: (value: string) => any, defaultValue?: any): Command;
  152 +
  153 +
  154 + /**
  155 + * Sets the command version
  156 + */
  157 + export function version(version: string): Command;
  158 +
  159 + /**
  160 + * Parse the arguments array and invokes the commands passing the parsed options.
  161 + * @param argv the arguments array.
  162 + */
  163 + export function parse(argv: string[]): Command;
  164 +
  165 + /**
  166 + * Gets or sets the command description.
  167 + * @param description the new description for the command. When ommited this returns the current description, otherwise returns the current Command.
  168 + */
  169 + export function description(description: string): Command;
  170 + export function description(): string;
  171 +
  172 + /**
  173 + * Gets or sets the usage help string.
  174 + */
  175 + export function usage(usage: string): Command;
  176 + export function usage(): string;
  177 +
  178 + /*
  179 + * Prompt the user for a value, calling the callback function.
  180 + *
  181 + * Supports single-line and multi-line prompts.
  182 + * To issue a single-line prompt simply add a whitespace
  183 + * to the end of label, something like "name: ", whereas
  184 + * for a multi-line prompt omit this "description:".
  185 + * @param label the label string to be printed in console.
  186 + * @param callback a callback function to handle the inputed string.
  187 + */
  188 + export function prompt(label: string, callback: (value: string) => any): void;
  189 +
  190 + export function promptForNumber(label: string, callback: (value: number) => any): void;
  191 + export function promptForDate(label: string, callback: (value: Date) => any): void;
  192 + export function promptSingleLine(label: string, callback: (value: string) => any): void;
  193 + export function promptMultiLine(label: string, callback: (value: string) => any): void;
  194 + /**
  195 + * Prompt for password with a label, a optional mask char and callback function.
  196 + * The mask string defaults to '', aka no output is written while typing, you may want to use "*" etc.
  197 + */
  198 + export function password(label: string, mask: string, callback: (value: string) => any): void;
  199 + export function password(label: string, callback: (value: string) => any): void;
  200 +
  201 + /**
  202 + * Prompts the user for a confirmation.
  203 + */
  204 + export function confirm(label: string, callback: (flag: bool) => any): void;
  205 +
  206 + /**
  207 + * Prompt for password with str, mask char and callback fn(val).
  208 + * The mask string defaults to '', aka no output is written while typing, you may want to use "*" etc.
  209 + */
  210 + export function choose(options: string[], callback: (idx: number) => any): void;
  211 + export function choose(options: any[], callback: (idx: number) => any): void;
  212 +
  213 + /**
  214 + * Add command with the specified name. Returns a new instance of Command.
  215 + *
  216 + * The .action() callback is invoked when the
  217 + * command name is specified via ARGV,
  218 + * and the remaining arguments are applied to the
  219 + * function for access.
  220 +
  221 + * When the name is "*" an un-matched command
  222 + * will be passed as the first arg, followed by
  223 + * the rest of ARGV remaining.
  224 + *
  225 + * @param name the name of the command. Pass "*" to trap un-matched commands.
  226 + */
  227 + export function command(name: string): Command;
  228 +}

0 comments on commit fce7051

Please sign in to comment.
Something went wrong with that request. Please try again.