Skip to content

Commit

Permalink
feat: show default flag/arg values
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Feb 2, 2018
1 parent fa1c6e0 commit 7bedad5
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 29 deletions.
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
},
"bugs": "https://github.com/anycli/plugin-help/issues",
"dependencies": {
"@anycli/command": "^0.3.6",
"@anycli/command": "^0.3.7",
"@anycli/screen": "^0.0.3",
"chalk": "^2.3.0",
"cli-ux": "^3.3.12",
"cli-ux": "^3.3.13",
"indent-string": "^3.2.0",
"lodash": "^4.17.4",
"string-width": "^2.1.1",
"widest-line": "^2.0.0",
"wrap-ansi": "^3.0.1"
},
"devDependencies": {
"@anycli/config": "^0.2.11",
"@anycli/engine": "^0.1.47",
"@anycli/plugin-plugins": "^0.2.3",
"@anycli/plugin-version": "^0.1.19",
"@anycli/config": "^0.3.0",
"@anycli/engine": "^0.3.0",
"@anycli/plugin-plugins": "^0.2.4",
"@anycli/plugin-version": "^0.1.20",
"@anycli/test": "^0.10.3",
"@anycli/tslint": "^0.2.2",
"@types/chai": "^4.1.2",
Expand Down
12 changes: 9 additions & 3 deletions src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class CommandHelp {
'$',
this.config.bin,
command.id,
command.args.map(a => this.arg(a)).join(' '),
command.args.filter(a => !a.hidden).map(a => this.arg(a)).join(' '),
flags.length && '[OPTIONS]',
])
.compact()
Expand All @@ -75,11 +75,14 @@ export default class CommandHelp {
}

protected args(args: ICachedCommand['args']): Section | undefined {
if (!args.find(f => !!f.description)) return
if (!args.length) return
return {
heading: 'arguments',
body: args.map(a => {
return [a.name!.toUpperCase(), a.description ? dim(a.description) : undefined]
const name = a.name.toUpperCase()
let description = a.description || ''
if (a.default) description = `[default: ${a.default}] ${description}`
return [name, description ? dim(description) : undefined]
})
}
}
Expand Down Expand Up @@ -112,6 +115,9 @@ export default class CommandHelp {
}

let right = flag.description || ''
if (flag.type === 'option' && flag.default) {
right = `[default: ${flag.default}] ${right}`
}
if (flag.required) right = `(required) ${right}`

return [left, dim(right.trim())]
Expand Down
2 changes: 1 addition & 1 deletion src/commands/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class HelpCommand extends Command {
format: flags.enum({description: 'output in a different format', options: ['markdown', 'man']}),
}
static args = [
{name: 'command', required: false}
{name: 'command', required: false, description: 'command to show help for'}
]

options = parse(this.argv, HelpCommand)
Expand Down
27 changes: 27 additions & 0 deletions test/command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,33 @@ ALIASES
OPTIONS
--myenum=(a|b|c)`))

test
.commandHelp(class extends Command {
static id = 'apps:create'
static args = [
{name: 'arg1', default: '.'},
{name: 'arg2', default: '.', description: 'arg2 desc'},
{name: 'arg3', description: 'arg3 desc'},
]
static flags = {
flag1: flags.string({default: '.'}),
flag2: flags.string({default: '.', description: 'flag2 desc'}),
flag3: flags.string({description: 'flag3 desc'}),
}
})
.it('outputs with default options', ctx => expect(ctx.commandHelp).to.equal(`USAGE
$ anycli apps:create [ARG1] [ARG2] [ARG3] [OPTIONS]
ARGUMENTS
ARG1 [default: .]
ARG2 [default: .] arg2 desc
ARG3 arg3 desc
OPTIONS
--flag1=flag1 [default: .]
--flag2=flag2 [default: .] flag2 desc
--flag3=flag3 flag3 desc`))

// class AppsCreate3 extends Command {
// static id = 'apps:create'
// static flags = {
Expand Down
3 changes: 3 additions & 0 deletions test/commands/help.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ describe('help command', () => {
USAGE
$ anycli help [COMMAND] [OPTIONS]
ARGUMENTS
COMMAND command to show help for
OPTIONS
--all see all commands in CLI
--format=(markdown|man) output in a different format
Expand Down
59 changes: 40 additions & 19 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
# yarn lockfile v1


"@anycli/command@^0.3.4":
version "0.3.4"
resolved "https://registry.yarnpkg.com/@anycli/command/-/command-0.3.4.tgz#c02b07f0f309192021cd76fa66b3f88db173025e"
"@anycli/command@^0.3.5", "@anycli/command@^0.3.7":
version "0.3.7"
resolved "https://registry.yarnpkg.com/@anycli/command/-/command-0.3.7.tgz#293c366ed8227fcdc675e278a46d8f93435526a5"
dependencies:
"@anycli/parser" "^3.0.4"
cli-ux "^3.3.12"
cli-ux "^3.3.13"
debug "^3.1.0"
lodash "^4.17.4"
tslib "^1.9.0"
Expand All @@ -22,9 +22,9 @@
lodash "^4.17.4"
tslib "^1.9.0"

"@anycli/config@^0.2.11":
version "0.2.11"
resolved "https://registry.yarnpkg.com/@anycli/config/-/config-0.2.11.tgz#1b043946b18e159e6e44195aa4fef309b79f8ede"
"@anycli/config@^0.3.0":
version "0.3.0"
resolved "https://registry.yarnpkg.com/@anycli/config/-/config-0.3.0.tgz#a0938488e7afc507d69ec13a8e52c71aaecc08ea"
dependencies:
cli-ux "^3.3.10"
debug "^3.1.0"
Expand All @@ -33,16 +33,17 @@
lodash "^4.17.4"
read-pkg "^3.0.0"

"@anycli/engine@^0.1.47":
version "0.1.47"
resolved "https://registry.yarnpkg.com/@anycli/engine/-/engine-0.1.47.tgz#c266e74ed3d1f372065d6329c7a08be04be158ff"
"@anycli/engine@^0.3.0":
version "0.3.0"
resolved "https://registry.yarnpkg.com/@anycli/engine/-/engine-0.3.0.tgz#a021569b567dbc9abdc37d5e6630774f34b1cd56"
dependencies:
"@anycli/manifest-file" "^0.3.8"
cli-ux "^3.3.12"
cli-ux "^3.3.13"
debug "^3.1.0"
fs-extra "^5.0.0"
globby "^7.1.1"
lodash "^4.17.4"
semver "^5.5.0"

"@anycli/manifest-file@^0.3.8":
version "0.3.8"
Expand All @@ -63,11 +64,11 @@
chalk "^2.3.0"
lodash "^4.17.4"

"@anycli/plugin-plugins@^0.2.3":
version "0.2.3"
resolved "https://registry.yarnpkg.com/@anycli/plugin-plugins/-/plugin-plugins-0.2.3.tgz#75bd2ef541e87c0ce875f4b018cec6ed7cc9ebab"
"@anycli/plugin-plugins@^0.2.4":
version "0.2.4"
resolved "https://registry.yarnpkg.com/@anycli/plugin-plugins/-/plugin-plugins-0.2.4.tgz#894c837e74bc8116f816404fb8de99e440e5410a"
dependencies:
"@anycli/command" "^0.3.4"
"@anycli/command" "^0.3.6"
"@anycli/manifest-file" "^0.3.8"
"@heroku-cli/color" "^1.1.3"
chalk "^2.3.0"
Expand All @@ -80,11 +81,11 @@
tslib "^1.9.0"
yarn "^1.3.2"

"@anycli/plugin-version@^0.1.19":
version "0.1.19"
resolved "https://registry.yarnpkg.com/@anycli/plugin-version/-/plugin-version-0.1.19.tgz#8a4e9b3e44acb810cb1db82db71bf9dc1048b297"
"@anycli/plugin-version@^0.1.20":
version "0.1.20"
resolved "https://registry.yarnpkg.com/@anycli/plugin-version/-/plugin-version-0.1.20.tgz#c0b3bc5711d33bc7a71ef0264611165aab64ae3c"
dependencies:
"@anycli/command" "^0.3.4"
"@anycli/command" "^0.3.5"
cli-ux "^3.3.12"

"@anycli/screen@^0.0.3":
Expand Down Expand Up @@ -412,6 +413,26 @@ cli-ux@^3.3.12:
strip-ansi "^4.0.0"
supports-color "^5.1.0"

cli-ux@^3.3.13:
version "3.3.13"
resolved "https://registry.yarnpkg.com/cli-ux/-/cli-ux-3.3.13.tgz#123e0c7a29d1f743447b919500a9055486992df6"
dependencies:
"@anycli/screen" "^0.0.3"
"@heroku/linewrap" "^1.0.0"
ansi-styles "^3.2.0"
cardinal "^1.0.0"
chalk "^2.3.0"
clean-stack "^1.3.0"
extract-stack "^1.0.0"
fs-extra "^5.0.0"
indent-string "^3.2.0"
lodash "^4.17.4"
node-notifier "^5.2.1"
password-prompt "^1.0.4"
semver "^5.5.0"
strip-ansi "^4.0.0"
supports-color "^5.1.0"

cli-width@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/cli-width/-/cli-width-2.2.0.tgz#ff19ede8a9a5e579324147b0c11f0fbcbabed639"
Expand Down

0 comments on commit 7bedad5

Please sign in to comment.