Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Positional options #1427

Merged
merged 26 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0f74c8b
First cut at optionsBeforeArguments
shadowspawn Dec 27, 2020
3de2bde
Different to mix global options and subcommands, and options and argu…
shadowspawn Dec 27, 2020
bc1f8f7
Different to mix global options and subcommands, and options and argu…
shadowspawn Dec 27, 2020
c93da58
Add _parseOptionsFollowingArguments
shadowspawn Dec 27, 2020
69458b4
Use allow wording
shadowspawn Dec 27, 2020
304ad64
Another try at naming
shadowspawn Dec 28, 2020
ee98c44
Exclude options from special processing, which fixes help
shadowspawn Dec 28, 2020
e155f3a
Add help checks for new option configuration
shadowspawn Dec 28, 2020
fe29f6e
Rename after discovering needed for any positional options
shadowspawn Dec 29, 2020
d781d77
Rework logic to hopefully cope with default commands.
shadowspawn Dec 29, 2020
eef4d52
Expand basic tests. Positional options are tricky!
shadowspawn Dec 29, 2020
f20aff0
Add first default command tests
shadowspawn Dec 30, 2020
91468d3
Fill out more tests
shadowspawn Dec 31, 2020
3b6cd3f
Add setters, and throw when passThrough without enabling positional
shadowspawn Jan 1, 2021
bb95593
Rename test file
shadowspawn Jan 1, 2021
9c5b77a
Add TypeScript
shadowspawn Jan 1, 2021
6a4524b
Add tests. Fix help handling by making explicit.
shadowspawn Jan 2, 2021
4d26a15
Reorder tests
shadowspawn Jan 2, 2021
4136ce8
Use usual indentation
shadowspawn Jan 2, 2021
17472b9
Make _enablePositionalOptions inherited to simpify nested commands
shadowspawn Jan 2, 2021
2c9373f
Add examples
shadowspawn Jan 2, 2021
ca47d95
Add tests for some less common setups
shadowspawn Jan 2, 2021
a021878
Test the boring true/false parameters
shadowspawn Jan 2, 2021
7ab4d6d
Fix typo
shadowspawn Jan 2, 2021
71d92e1
Add new section to README with parsing configuration.
shadowspawn Jan 3, 2021
6f7ffcb
Tweak wording in README
shadowspawn Jan 3, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add examples
  • Loading branch information
shadowspawn committed Jan 2, 2021
commit 2c9373ff18f9910ac7a2f223332d871a7903dd63
23 changes: 23 additions & 0 deletions examples/pass-through-options.js
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
27 changes: 27 additions & 0 deletions examples/positional-options.js
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