Skip to content

Commit 812048c

Browse files
federicojassonBenjamin E. Coe
authored andcommitted
feat: support defaultDescription for positional arguments
1 parent a6e67f1 commit 812048c

File tree

4 files changed

+129
-2
lines changed

4 files changed

+129
-2
lines changed

docs/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,6 +1234,7 @@ Valid `opt` keys include:
12341234
- `coerce`: function, coerce or transform parsed command line values into another value, see [`coerce()`](#coerce)
12351235
- `conflicts`: string or object, require certain keys not to be set, see [`conflicts()`](#conflicts)
12361236
- `default`: value, set a default value for the option, see [`default()`](#default)
1237+
- `defaultDescription`: string, use this description for the default value in help content, see [`default()`](#default)
12371238
- `desc`/`describe`/`description`: string, the option description for help content, see [`describe()`](#describe)
12381239
- `implies`: string or object, require certain keys to be set, see [`implies()`](#implies)
12391240
- `normalize`: boolean, apply `path.normalize()` to the option, see [`normalize()`](#normalize)

test/usage.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2095,6 +2095,118 @@ describe('usage tests', () => {
20952095
r.logs[0].should.include('default: 80 for HTTP and 443 for HTTPS')
20962096
})
20972097
})
2098+
2099+
describe('using positional() without default()', () => {
2100+
it('should output given desc with default value', () => {
2101+
const r = checkUsage(() => yargs(['url', '-h'])
2102+
.help('h')
2103+
.command('url', 'Print a URL', (yargs) => {
2104+
yargs.positional('port', {
2105+
describe: 'The port value for URL',
2106+
defaultDescription: '80 for HTTP and 443 for HTTPS',
2107+
default: 80
2108+
})
2109+
})
2110+
.wrap(null)
2111+
.parse()
2112+
)
2113+
2114+
r.logs[0].should.include('default: 80 for HTTP and 443 for HTTPS')
2115+
})
2116+
2117+
it('should output given desc without default value', () => {
2118+
const r = checkUsage(() => yargs(['url', '-h'])
2119+
.help('h')
2120+
.command('url', 'Print a URL', (yargs) => {
2121+
yargs.positional('port', {
2122+
describe: 'The port value for URL',
2123+
defaultDescription: '80 for HTTP and 443 for HTTPS'
2124+
})
2125+
})
2126+
.wrap(null)
2127+
.parse()
2128+
)
2129+
2130+
r.logs[0].should.include('default: 80 for HTTP and 443 for HTTPS')
2131+
})
2132+
2133+
it('should prefer given desc over function desc', () => {
2134+
const r = checkUsage(() => yargs(['url', '-h'])
2135+
.help('h')
2136+
.command('url', 'Print a URL', (yargs) => {
2137+
yargs.positional('port', {
2138+
describe: 'The port value for URL',
2139+
defaultDescription: '80 for HTTP and 443 for HTTPS',
2140+
default: function determinePort () {
2141+
return 80
2142+
}
2143+
})
2144+
})
2145+
.wrap(null)
2146+
.parse()
2147+
)
2148+
2149+
r.logs[0].should.include('default: 80 for HTTP and 443 for HTTPS')
2150+
})
2151+
})
2152+
2153+
describe('using positional() with default()', () => {
2154+
it('should prefer default() desc when given last', () => {
2155+
const r = checkUsage(() => yargs(['url', '-h'])
2156+
.help('h')
2157+
.command('url', 'Print a URL', (yargs) => {
2158+
yargs
2159+
.positional('port', {
2160+
describe: 'The port value for URL',
2161+
defaultDescription: 'depends on protocol'
2162+
})
2163+
.default('port', null, '80 for HTTP and 443 for HTTPS')
2164+
})
2165+
.wrap(null)
2166+
.parse()
2167+
)
2168+
2169+
r.logs[0].should.include('default: 80 for HTTP and 443 for HTTPS')
2170+
})
2171+
2172+
it('should prefer positional() desc when given last', () => {
2173+
const r = checkUsage(() => yargs(['url', '-h'])
2174+
.help('h')
2175+
.command('url', 'Print a URL', (yargs) => {
2176+
yargs
2177+
.default('port', null, '80 for HTTP and 443 for HTTPS')
2178+
.positional('port', {
2179+
describe: 'The port value for URL',
2180+
defaultDescription: 'depends on protocol'
2181+
})
2182+
})
2183+
.wrap(null)
2184+
.parse()
2185+
)
2186+
2187+
r.logs[0].should.include('default: depends on protocol')
2188+
})
2189+
2190+
it('should prefer positional() desc over default() function', () => {
2191+
const r = checkUsage(() => yargs(['url', '-h'])
2192+
.help('h')
2193+
.command('url', 'Print a URL', (yargs) => {
2194+
yargs
2195+
.positional('port', {
2196+
describe: 'The port value for URL',
2197+
defaultDescription: '80 for HTTP and 443 for HTTPS'
2198+
})
2199+
.default('port', function determinePort () {
2200+
return 80
2201+
})
2202+
})
2203+
.wrap(null)
2204+
.parse()
2205+
)
2206+
2207+
r.logs[0].should.include('default: 80 for HTTP and 443 for HTTPS')
2208+
})
2209+
})
20982210
})
20992211

21002212
describe('normalizeAliases', () => {

test/yargs.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,6 +2046,20 @@ describe('yargs dsl tests', () => {
20462046
argv.heroes.should.eql(['batman', 'Iron Man'])
20472047
})
20482048

2049+
it('allows a defaultDescription to be set', () => {
2050+
yargs('cmd')
2051+
.command('cmd [heroes...]', 'a command', (yargs) => {
2052+
yargs.positional('heroes', {
2053+
default: ['batman', 'Iron Man'],
2054+
defaultDescription: 'batman and Iron Man'
2055+
})
2056+
}).parse()
2057+
2058+
yargs.getOptions().defaultDescription.should.deep.equal({
2059+
heroes: 'batman and Iron Man'
2060+
})
2061+
})
2062+
20492063
it('allows an implied argument to be specified', (done) => {
20502064
yargs()
20512065
.command('cmd <hero>', 'a command', (yargs) => {

yargs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,8 @@ function Yargs (processArgs, cwd, parentRequire) {
694694
}
695695

696696
// .positional() only supports a subset of the configuration
697-
// options availble to .option().
698-
const supportedOpts = ['default', 'implies', 'normalize',
697+
// options available to .option().
698+
const supportedOpts = ['default', 'defaultDescription', 'implies', 'normalize',
699699
'choices', 'conflicts', 'coerce', 'type', 'describe',
700700
'desc', 'description', 'alias']
701701
opts = objFilter(opts, (k, v) => {

0 commit comments

Comments
 (0)