meow icon indicating copy to clipboard operation
meow copied to clipboard

Autogenerate flag help when no helpMessage is given

Open matthewadams opened this issue 6 years ago • 5 comments

I'm coming from commander, which autogenerates documentation on flags, and was surprised to see that meow's autoHelp feature doesn't do the same.

// in index.js
const cli = require('meow')({
  flags: {
    'initial-indent': {
      type: 'string',
      alias: 'n',
      description: 'Initial indentation level'
      default: '0'
    },
    indent: {
      type: 'string',
      alias: 'i',
      description: 'YAML indentation level'
      default: '2'
    }
  }
})
cli.showHelp()

should generate output something like

$ ./index.js

  My cool tool description

  Options:
    -n, --initial-indent  [value]  Initial indentation spaces.  Optional; default 0.
    -i, --indent          [value]  YAML indentation spaces.  Optional; default 2.

matthewadams avatar Mar 15 '18 21:03 matthewadams

Meow was initially just created to be a small helper lib for making tiny CLI's. It has now outgrown that, but it never had ambitions of being a Commander replacement.

I wouldn't mind having this though.

sindresorhus avatar Mar 16 '18 04:03 sindresorhus

Can this be merged? Its a neat feature @sindresorhus?

lukasoppermann avatar Nov 27 '18 14:11 lukasoppermann

https://github.com/sindresorhus/meow/pull/82 was closed as I got no response. If anyone wants to work on this, you can probably continue where that PR left off. Make sure to address the feedback given there.

sindresorhus avatar Nov 17 '19 06:11 sindresorhus

I've created a new PR #140!

ybiquitous avatar Mar 22 '20 13:03 ybiquitous

I'd love this as well. If not possible to completely auto-generate, maybe consider the option to have a helpMessage object instead of a string, like so:

const cli = meow({
    description: 'A tool that does stuff with files and encodings',
    inputs: ['<file>', '<encoding>'], // command name is auto-prepended based on process.argv
    options: { // alternatively add the flag description to the flags object and make this property a boolean switch
        '-c': 'Convert file encoding', // long option name auto-added based on flags object
        '--dry-run': 'Output commands instead of executing', // option alias auto-added if available
        '--help': 'Show this help message',
    },
    examples: {
        'input.txt utf-8': 'Loads this file and converts to UTF-8', // command name is auto-prepended based on process.argv
        '—dry-run other_file.csv': 'Outputs the file to stdout',
   }
},
{
    importMeta: import.meta,
    flags: { … }
})

verheyenkoen avatar Apr 08 '22 19:04 verheyenkoen