javascriptnode.jscommand-line-interfaceflagsnode-commander

args of short flag and long flag are parsed differently in commander module node js


program
  .command("split")
  .description("Split a string into substrings and display as an array")
  .argument("<string>", "string to split")
  .option("--first", "display just the first substring")
  .option("-s ,--separator <char>", "separator character", ",")
  .action((str, options) => {
  console.log(options);
  const limit = options.first ? 1 : undefined;
  console.log(str.split(options.separator, limit));
 });

-s="l" is parsed as {seperator:"=l"}

--seperator="l" is parsed as {seperator :"l"}

how to get uniform parsing either as "=l" or "l" while using both short and long flags


Solution

  • The supported forms for specifying an option and option-argument in Commander are

    serve -p 80
    serve --port 80
    serve -p80
    serve --port=80
    

    There is not support in Commander for making the short and long options "uniform" for the combined option and value form.

    The short form comes from POSIX and GNU guidelines.

    POSIX Utility Conventions

    If the SYNOPSIS of a standard utility shows an option with a mandatory option-argument (as with [ -c option_argument] in the example), a conforming application shall use separate arguments for that option and its option-argument. However, a conforming implementation shall also permit applications to specify the option and option-argument in the same argument string without intervening characters.

    GNU Program Argument Syntax Conventions

    An option and its argument may or may not appear as separate tokens. (In other words, the whitespace separating them is optional.) Thus, -o foo and -ofoo are equivalent.

    (I am a maintainer of Commander.)