Git Product home page Git Product logo

Comments (2)

nexdrew avatar nexdrew commented on June 15, 2024

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 or paramsDesc: string or array of strings, shortcut to add description for positional args without defining params

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.

davidkjackson54 avatar davidkjackson54 commented on June 15, 2024

Thanks for clarifying this. Really appreciate the help.

from sywac.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.