Comments (2)
You have to remember that the object you're exporting represents the command, not its params, which means your sample code is attempting to assign a default value to the command itself, which doesn't make sense.
To more fully configure the params (positionals) for the command, use the params
property (array of objects) instead of paramsDesc
(array of strings).
Note that, as stated in the docs, paramsDesc
is just a shortcut to use when the only thing you need to configure in params
would be a description.
paramsDescription
orparamsDesc
: string or array of strings, shortcut to add description for positional args without definingparams
Thus the following are equivalent:
paramsDesc: ['Some description']
params: [{ desc: 'Some description' }]
With that being said, we can fix your sample code by replacing paramsDesc
with params
and applying the defaultValue
to the param configuration object.
Note that it doesn't really make sense to say a param is required when providing a default value, because you want the default value to be used when the argument is not given instead of requiring the user to provide the value.
Here are a couple ways of accomplishing this.
The first one changes the way the positional is declared in the flags
DSL string, to remove the implicit requirement by using []
instead of <>
:
module.exports = {
flags: 'pkglist [-p|--pkg] [package]',
desc: 'Package List.',
params: [{
desc: 'Package Name',
defaultValue: '*'
}],
run: a => console.log(`list package ${a.package}`)
}
$ ./issue29.js pkglist -h
Usage: issue29 pkglist [-p|--pkg] [package] [options]
Arguments:
[-p|--pkg] [package] Package Name [string] [default: *]
Global Options:
-h, --help Show help [boolean]
$ ./issue29.js pkglist
list package *
$ ./issue29.js pkglist x
list package x
The second one keeps the flags
DSL the way you have it (using <>
) but explicitly marks the param as not required:
module.exports = {
flags: 'pkglist [-p|--pkg] <package>',
desc: 'Package List.',
params: [{
desc: 'Package Name',
required: false,
defaultValue: '*'
}],
run: a => console.log(`list package ${a.package}`)
}
$ ./issue29.js pkglist -h
Usage: issue29 pkglist [-p|--pkg] <package> [options]
Arguments:
[-p|--pkg] <package> Package Name [string] [default: *]
Global Options:
-h, --help Show help [boolean]
$ ./issue29.js pkglist
list package *
$ ./issue29.js pkglist x
list package x
from sywac.
Thanks for clarifying this. Really appreciate the help.
from sywac.
Related Issues (20)
- How to specify certain options are required? HOT 3
- cliMessage usage instructions for command print with wrong context? HOT 1
- How to prevent unknown options HOT 2
- Should certain stdout messages go to stderr? HOT 3
- Respect ANSI codes when applying maxWidth HOT 3
- How to provide commands with additional data? HOT 5
- Default commands with positional args behave unexpectedly HOT 6
- Control style with a flag HOT 3
- Best practice for error output? HOT 14
- Proposal for "run" handler at Api level HOT 8
- Customize "--" flag that delimits extra arguments HOT 2
- Convert kebab-case flags to camelCase HOT 2
- feature: document exit codes in help text
- Any typings / d.ts files for sywac? HOT 4
- Best way to add implicitcommands? HOT 3
- Prep for Hacktoberfest HOT 3
- Welcome, Hacktoberfest 2020!
- Add "--no-flag" support for Boolean types HOT 5
- Trying to get in touch regarding a security issue HOT 7
- Better error generation if file doesn't exist
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sywac.