説明なし

sub_commands.js 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env node
  2. 'use strict';
  3. var ArgumentParser = require('../lib/argparse').ArgumentParser;
  4. var parser = new ArgumentParser({
  5. version: '0.0.1',
  6. addHelp: true,
  7. description: 'Argparse examples: sub-commands'
  8. });
  9. var subparsers = parser.addSubparsers({
  10. title: 'subcommands',
  11. dest: "subcommand_name"
  12. });
  13. var bar = subparsers.addParser('c1', {addHelp: true, help: 'c1 help'});
  14. bar.addArgument(
  15. [ '-f', '--foo' ],
  16. {
  17. action: 'store',
  18. help: 'foo3 bar3'
  19. }
  20. );
  21. var bar = subparsers.addParser(
  22. 'c2',
  23. {aliases: ['co'], addHelp: true, help: 'c2 help'}
  24. );
  25. bar.addArgument(
  26. [ '-b', '--bar' ],
  27. {
  28. action: 'store',
  29. type: 'int',
  30. help: 'foo3 bar3'
  31. }
  32. );
  33. parser.printHelp();
  34. console.log('-----------');
  35. var args;
  36. args = parser.parseArgs('c1 -f 2'.split(' '));
  37. console.dir(args);
  38. console.log('-----------');
  39. args = parser.parseArgs('c2 -b 1'.split(' '));
  40. console.dir(args);
  41. console.log('-----------');
  42. args = parser.parseArgs('co -b 1'.split(' '));
  43. console.dir(args);
  44. console.log('-----------');
  45. parser.parseArgs(['c1', '-h']);