meow icon indicating copy to clipboard operation
meow copied to clipboard

Idea: adopt docopt

Open marionebl opened this issue 7 years ago • 6 comments

It would be awesome if meow could adopt http://docopt.org/ to deduce additional behaviour from the help text. This could be

  • disabled by default
  • help with #69
  • add more validation to user input
  • ...

marionebl avatar Oct 18 '17 21:10 marionebl

That was initially the long-term plan, but after having used Swift for a while, I've come to appreciate type safety, so now I'm leaning more towards going the other way, and generating the help text from APIs instead. No matter how well specified something is, parsing will have ambiguities.

sindresorhus avatar Oct 19 '17 07:10 sindresorhus

Understood. Are you aiming for a declarative API or something like yargs?

marionebl avatar Oct 19 '17 07:10 marionebl

Something like yargs, but less comprehensive and opinionated, and not a chainable API.

sindresorhus avatar Oct 19 '17 07:10 sindresorhus

Pretty much just expanding on the existing options object:

{
	flags: {
		rainbow: {
			type: 'boolean',
			alias: 'r'
		}
	}
}

I imagine we could add more options there, and a way to define command and examples too.

sindresorhus avatar Oct 19 '17 07:10 sindresorhus

hi,

just wanted to post here, maybe it's of use to anyone else.

just build my quick and rough self generating usage message from the flags array

would be lovely to have something like this already included

thank you for this package, it s lovely

const meow = require("meow")

// shorthand for default values
const defaultValue = s => flags[s].default
const options = f => {
  let r = ""
  let l = 10
  let s = " "
  console.log(...f)
  let fullFlags = {
    help: { description: "This message" },
    ...f,
    version: { description: "Display app version and quit" },
  }

  for (let [name, flag] of Object.entries(fullFlags)) {
    l = Math.max(l, name.length)
  }
  console.log("max", l)
  for (let [name, flag] of Object.entries(fullFlags)) {
    console.log(name, flag)
    let i = `  --${name}`
    i += flag.type ? ` ${flag.type}` : ``
    r += i
    r += s.repeat(l + 15 - i.length)
    r += `${flag.description}`
    r += flag.default ? ` (default ${flag.default})` : ``
    r += `\n`
  }
  return r
}

const flags = {
  "metrics-port": {
    type: "int",
    default: 8888,
    description: "Prometheus metrics port",
  },
  "redis-server": {
    type: "string",
    default: "localhost:6379",
    description: "Redis host:port",
  },
  "deepstream-prefix": {
    type: "string",
    default: "pending",
    description: "Namespace for all deepstream data",
  },
}

const usage = `
Usage
  $ pending-transactions-deepstream [options]

Options
${options(flags)}
`

const cli = meow(usage, { flags })

tzapu avatar Jan 23 '18 08:01 tzapu

We should still add a link to http://docopt.org/, to encourage better descriptions for new developers. Or if you have another standard that you like more, I would be interested to hear that.

synox avatar Oct 13 '21 08:10 synox