Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* First cut at optionsBeforeArguments * Different to mix global options and subcommands, and options and arguments. * Different to mix global options and subcommands, and options and arguments. * Add _parseOptionsFollowingArguments * Use allow wording * Another try at naming * Exclude options from special processing, which fixes help * Add help checks for new option configuration * Rename after discovering needed for any positional options * Rework logic to hopefully cope with default commands. * Expand basic tests. Positional options are tricky! * Add first default command tests * Fill out more tests * Add setters, and throw when passThrough without enabling positional * Rename test file * Add TypeScript * Add tests. Fix help handling by making explicit. * Reorder tests * Use usual indentation * Make _enablePositionalOptions inherited to simpify nested commands * Add examples * Add tests for some less common setups * Test the boring true/false parameters * Fix typo * Add new section to README with parsing configuration. * Tweak wording in README
- Loading branch information
1 parent
1383870
commit 8ac84ec
Showing
8 changed files
with
702 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env node | ||
|
||
// const { Command } = require('commander'); // (normal include) | ||
const { Command } = require('../'); // include commander in git clone of commander repo | ||
const program = new Command(); | ||
|
||
program | ||
.arguments('<utility> [args...]') | ||
.passThroughOptions() | ||
.option('-d, --dry-run') | ||
.action((utility, args, options) => { | ||
const action = options.dryRun ? 'Would run' : 'Running'; | ||
console.log(`${action}: ${utility} ${args.join(' ')}`); | ||
}); | ||
|
||
program.parse(); | ||
|
||
// Try the following: | ||
// | ||
// node pass-through-options.js git status | ||
// node pass-through-options.js git --version | ||
// node pass-through-options.js --dry-run git checkout -b new-branch | ||
// node pass-through-options.js git push --dry-run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env node | ||
|
||
// const { Command } = require('commander'); // (normal include) | ||
const { Command } = require('../'); // include commander in git clone of commander repo | ||
const program = new Command(); | ||
|
||
program | ||
.enablePositionalOptions() | ||
.option('-p, --progress'); | ||
|
||
program | ||
.command('upload <file>') | ||
.option('-p, --port <number>', 'port number', 80) | ||
.action((file, options) => { | ||
if (program.opts().progress) console.log('Starting upload...'); | ||
console.log(`Uploading ${file} to port ${options.port}`); | ||
if (program.opts().progress) console.log('Finished upload'); | ||
}); | ||
|
||
program.parse(); | ||
|
||
// Try the following: | ||
// | ||
// node positional-options.js upload test.js | ||
// node positional-options.js -p upload test.js | ||
// node positional-options.js upload -p 8080 test.js | ||
// node positional-options.js -p upload -p 8080 test.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.