[cli] Migrate alias command to use parseArguments and getFlagsSpecification (#11467)

Migrating `vc alias` to use new argument parsing utilities.

Note that our snapshot tests show that our help text was out of sync with what we were actually parsing from the user. 

In particular, we were showing `-n` as the shorthand for both `next` and `limit` and neither of these had this shortflag. `next` was parsing `-N` (capital), so I've updated it to that.

`--json` was parsed, but wasn't mentioned in the help text. I've kept it this way, but it may be worth adding a description. We may want to have a separate discussion about the nature and role of `json` output from our CLI as a whole.
This commit is contained in:
Erika Rowland
2024-04-24 12:48:04 -07:00
committed by GitHub
parent 5758838d09
commit 0f12005a68
4 changed files with 35 additions and 36 deletions

View File

@@ -52,7 +52,7 @@ export const aliasCommand: Command = {
name: 'next',
description: 'Show next page of results',
argument: 'MS',
shorthand: 'n',
shorthand: 'N',
type: String,
deprecated: false,
},
@@ -65,13 +65,14 @@ export const aliasCommand: Command = {
},
{
name: 'limit',
shorthand: 'n',
shorthand: null,
description:
'Number of results to return per page (default: 20, max: 100)',
argument: 'NUMBER',
type: String,
deprecated: false,
},
{ name: 'json', shorthand: null, type: Boolean, deprecated: false },
],
examples: [
{

View File

@@ -1,12 +1,13 @@
import { handleError } from '../../util/error';
import Client from '../../util/client';
import getArgs from '../../util/get-args';
import { parseArguments } from '../../util/get-args';
import getSubcommand from '../../util/get-subcommand';
import { help } from '../help';
import ls from './ls';
import rm from './rm';
import set from './set';
import { aliasCommand } from './command';
import { getFlagsSpecification } from '../../util/get-flags-specification';
const COMMAND_CONFIG = {
default: ['set'],
@@ -16,35 +17,33 @@ const COMMAND_CONFIG = {
};
export default async function alias(client: Client) {
let argv;
let parsedArguments;
const flagsSpecification = getFlagsSpecification(aliasCommand.options);
try {
argv = getArgs(client.argv.slice(2), {
'--json': Boolean,
'--yes': Boolean,
'--next': Number,
'--limit': Number,
'-y': '--yes',
'-N': '--next',
});
parsedArguments = parseArguments(client.argv.slice(2), flagsSpecification);
} catch (err) {
handleError(err);
return 1;
}
if (argv['--help']) {
if (parsedArguments.flags['--help']) {
client.output.print(help(aliasCommand, { columns: client.stderr.columns }));
return 2;
}
const { subcommand, args } = getSubcommand(argv._.slice(1), COMMAND_CONFIG);
const { subcommand, args } = getSubcommand(
parsedArguments.args.slice(1),
COMMAND_CONFIG
);
switch (subcommand) {
case 'ls':
return ls(client, argv, args);
return ls(client, parsedArguments.flags, args);
case 'rm':
return rm(client, argv, args);
return rm(client, parsedArguments.flags, args);
default:
return set(client, argv, args);
return set(client, parsedArguments.flags, args);
}
}

View File

@@ -24,18 +24,16 @@ exports[`help command > alias help output snapshots > alias help column width 40
Options:
-n, --limit <NUMBER> Number of
results to
return per page
(default: 20,
max: 100)
-n, --next <MS> Show next page
of results
-y, --yes Skip the
confirmation
prompt when
removing an
alias
--limit <NUMBER> Number of results to
return per page
(default: 20, max:
100)
-N, --next <MS> Show next page of
results
-y, --yes Skip the
confirmation prompt
when removing an
alias
Global Options:
@@ -108,10 +106,9 @@ exports[`help command > alias help output snapshots > alias help column width 80
Options:
-n, --limit <NUMBER> Number of results to return per page (default: 20, max:
100)
-n, --next <MS> Show next page of results
-y, --yes Skip the confirmation prompt when removing an alias
--limit <NUMBER> Number of results to return per page (default: 20, max: 100)
-N, --next <MS> Show next page of results
-y, --yes Skip the confirmation prompt when removing an alias
Global Options:
@@ -160,9 +157,9 @@ exports[`help command > alias help output snapshots > alias help column width 12
Options:
-n, --limit <NUMBER> Number of results to return per page (default: 20, max: 100)
-n, --next <MS> Show next page of results
-y, --yes Skip the confirmation prompt when removing an alias
--limit <NUMBER> Number of results to return per page (default: 20, max: 100)
-N, --next <MS> Show next page of results
-y, --yes Skip the confirmation prompt when removing an alias
Global Options: