-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1622 from alphagov/parse-cli-args-better
Better cli argument parsing
- Loading branch information
Showing
4 changed files
with
233 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module.exports = { | ||
parse: (argvInput) => { | ||
const args = [...argvInput].splice(2) | ||
const options = {} | ||
const paths = [] | ||
let command | ||
let contextFromPrevious | ||
|
||
const processOptionName = (unprocessed) => { | ||
if (unprocessed.startsWith('--')) { | ||
return unprocessed.substring(2) | ||
} | ||
if (unprocessed.startsWith('-')) { | ||
return unprocessed.substring(1) | ||
} | ||
} | ||
|
||
args.forEach(arg => { | ||
if (arg.startsWith('-')) { | ||
if (arg.includes('=')) { | ||
const parts = arg.split('=') | ||
options[processOptionName(parts[0])] = parts[1] | ||
return | ||
} | ||
contextFromPrevious = { | ||
option: arg | ||
} | ||
return | ||
} | ||
if (contextFromPrevious && contextFromPrevious.option) { | ||
options[processOptionName(contextFromPrevious.option)] = arg | ||
contextFromPrevious = undefined | ||
return | ||
} | ||
if (command) { | ||
paths.push(arg) | ||
} else { | ||
command = arg | ||
} | ||
}) | ||
|
||
return { | ||
command, | ||
options, | ||
paths | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
const parser = require('./argvParser') | ||
|
||
const addStandardArgs = arr => [ | ||
'/Users/user.name/.nvm/versions/node/node.version/bin/node', | ||
'/Users/user.name/.nvm/versions/node/node.version/bin/govuk-prototype-kit', | ||
...arr | ||
] | ||
|
||
describe('argv parser', () => { | ||
it('should parse a basic command', () => { | ||
const result = parser.parse(addStandardArgs(['hello'])) | ||
|
||
expect(result).toEqual({ | ||
command: 'hello', | ||
options: {}, | ||
paths: [] | ||
}) | ||
}) | ||
it('should parse a different basic command', () => { | ||
const result = parser.parse(addStandardArgs(['goodbye'])) | ||
|
||
expect(result).toEqual({ | ||
command: 'goodbye', | ||
options: {}, | ||
paths: [] | ||
}) | ||
}) | ||
it('should parse an option with double-hyphen', () => { | ||
const result = parser.parse(addStandardArgs([ | ||
'--name', | ||
'Alex', | ||
'hello' | ||
])) | ||
|
||
expect(result).toEqual({ | ||
command: 'hello', | ||
options: { | ||
name: 'Alex' | ||
}, | ||
paths: [] | ||
}) | ||
}) | ||
it('should handle multiple parameters with double-hyphens', () => { | ||
const result = parser.parse(addStandardArgs([ | ||
'--name', | ||
'Alex', | ||
'--pronouns', | ||
'they/them', | ||
'hello' | ||
])) | ||
|
||
expect(result).toEqual({ | ||
command: 'hello', | ||
options: { | ||
name: 'Alex', | ||
pronouns: 'they/them' | ||
}, | ||
paths: [] | ||
}) | ||
}) | ||
it('should handle paths after the command', () => { | ||
const result = parser.parse(addStandardArgs([ | ||
'create', | ||
'/tmp/abc' | ||
])) | ||
|
||
expect(result).toEqual({ | ||
command: 'create', | ||
options: {}, | ||
paths: ['/tmp/abc'] | ||
}) | ||
}) | ||
it('should support the longest command we currently have', () => { | ||
const result = parser.parse(addStandardArgs([ | ||
'--version', | ||
'local', | ||
'create', | ||
'/tmp/hello-world' | ||
])) | ||
|
||
expect(result).toEqual({ | ||
command: 'create', | ||
options: { | ||
version: 'local' | ||
}, | ||
paths: ['/tmp/hello-world'] | ||
}) | ||
}) | ||
it('should support options between the command and path(s)', () => { | ||
const result = parser.parse(addStandardArgs([ | ||
'create', | ||
'--version', | ||
'local', | ||
'/tmp/hello-world' | ||
])) | ||
|
||
expect(result).toEqual({ | ||
command: 'create', | ||
options: { | ||
version: 'local' | ||
}, | ||
paths: ['/tmp/hello-world'] | ||
}) | ||
}) | ||
it('should support single-hyphen options', () => { | ||
const result = parser.parse(addStandardArgs([ | ||
'create', | ||
'-v', | ||
'local', | ||
'/tmp/hello-world' | ||
])) | ||
|
||
expect(result).toEqual({ | ||
command: 'create', | ||
options: { | ||
v: 'local' | ||
}, | ||
paths: ['/tmp/hello-world'] | ||
}) | ||
}) | ||
it('should support options after path(s)', () => { | ||
const result = parser.parse(addStandardArgs([ | ||
'create', | ||
'/tmp/hello-world', | ||
'--version', | ||
'local' | ||
])) | ||
|
||
expect(result).toEqual({ | ||
command: 'create', | ||
options: { | ||
version: 'local' | ||
}, | ||
paths: ['/tmp/hello-world'] | ||
}) | ||
}) | ||
it('should support semvar numbers as values', () => { | ||
const result = parser.parse(addStandardArgs([ | ||
'--version', | ||
'13.0.1', | ||
'create', | ||
'/tmp/hello-world' | ||
])) | ||
|
||
expect(result).toEqual({ | ||
command: 'create', | ||
options: { | ||
version: '13.0.1' | ||
}, | ||
paths: ['/tmp/hello-world'] | ||
}) | ||
}) | ||
it('should support equals to set an option', () => { | ||
const result = parser.parse(addStandardArgs([ | ||
'--version=local', | ||
'create', | ||
'/tmp/hello-world' | ||
])) | ||
|
||
expect(result).toEqual({ | ||
command: 'create', | ||
options: { | ||
version: 'local' | ||
}, | ||
paths: ['/tmp/hello-world'] | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters