I’m facing this issue with yargs
where the combined flag -hx
seems to trigger the help menu instead of an error, even though -h
is intended to be a help flag and -x
is an alias for another option.
import chalk from "chalk";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import {
ArgumentColor,
HeadingColor,
SnippetColor,
SubHeadingColor,
ValueColor,
} from "../constants";
// Color-related functions
const colorize = {
subHeading: (text: string) => chalk.hex(SubHeadingColor)(text),
argument: (text: string) => chalk.hex(ArgumentColor)(text),
value: (text: string) => chalk.hex(ValueColor)(text),
heading: (text: string) => chalk.hex(HeadingColor)(text),
snippet: (text: string) => chalk.hex(SnippetColor)(text),
};
// Create colored argument
const createColoredArg = (arg: string, value: string) =>
`${colorize.argument(arg)}${colorize.value(value)}`;
// Command examples
const commandExamples = {
createDefaultStack: ($0: string, args: string[]) =>
`${colorize.subHeading("Command to Create Stack by provided Defaults:n")}${$0} ${args.join(" ")}`,
overrideDefaults: ($0: string, args: string[]) =>
`n${colorize.subHeading("Command to Override Default Parameters:n")}${$0} ${args.join(" ")}`,
useConfigFile: ($0: string, arg: string) =>
`n${colorize.subHeading("Command to use file for all arguments:n")}${$0} ${arg}`,
createAndCheckStatus: ($0: string, args: string[]) =>
`n${colorize.subHeading("Command to Create Stack and Check Status:n")}${$0} ${args.join(" ")}`,
createAndDeleteOnFailure: ($0: string, args: string[]) =>
`n${colorize.subHeading("Command to Create Stack and Delete on Failure:n")}${$0} ${args.join(" ")}`,
deleteStack: ($0: string, args: string[]) =>
`n${colorize.subHeading("Command to Delete Stack:n")}${$0} ${args.join(" ")}`,
};
// yargs configuration
const configureYargs = () => {
const $0 = colorize.argument("npx @invisinet/stackhub");
const commonArgs = [
createColoredArg("--region=", "<region>"),
createColoredArg("--stack=", "<stack-name>"),
createColoredArg("--access=", "<access-key-id>"),
createColoredArg("--secret=", "<secret-access-key>"),
];
return yargs(hideBin(process.argv))
.strict()
.strictOptions(true)
.strictCommands(true)
.showHelpOnFail(
false,
`${colorize.heading("Refer Package Documentation or Exec Below Command")}n${colorize.snippet("npx @invisinet/stackhub --help")}`,
)
.help("help")
.alias("help", "h")
.version(
"version",
"Show Version Info",
process.env?.npm_package_version ?? "",
)
.alias("version", "v")
.wrap(110)
.usage("npx @invisinet/stackhub [COMMANDS] [OPTIONS]")
.example([
[commandExamples.createDefaultStack($0, commonArgs)],
[
commandExamples.overrideDefaults($0, [
...commonArgs,
createColoredArg("--defaults=", "</path/to/CustomDefaults.json>"),
]),
],
[
commandExamples.useConfigFile(
$0,
createColoredArg("--config=", "</path/to/CustomConfig.json>"),
),
],
[
commandExamples.createAndCheckStatus($0, [
...commonArgs,
colorize.argument("--check"),
]),
],
[
commandExamples.createAndDeleteOnFailure($0, [
...commonArgs,
colorize.argument("--deleteOnFailure"),
]),
],
[
commandExamples.deleteStack($0, [
...commonArgs,
colorize.argument("--terminate"),
]),
],
])
.alias({
region: ["r"],
stack: ["s", "stackName"],
access: ["a", "accessKeyId"],
secret: ["x", "secretAccessKey"],
defaults: ["d", "overrideParameterDefaults"],
config: ["c", "configuration"],
check: ["k", "checkStatusAsync"],
cleanUp: ["f", "deleteOnFailure"],
delete: ["t", "terminate"],
})
.options({
region: {
type: "string",
demandOption: true,
describe: "AWS Region Name",
},
stack: {
type: "string",
demandOption: true,
describe: "Desired Stack Name in Alphanumeric",
},
access: {
type: "string",
demandOption: true,
describe: "Access Key ID Provided by Invisinet",
},
secret: {
type: "string",
demandOption: true,
describe: "Secret Access Key Provided by Invisinet",
},
defaults: {
type: "string",
demandOption: false,
describe: "Override the Defaults By JSON or YAML",
},
check: {
type: "boolean",
demandOption: false,
describe: "Flag to track stack events until completion",
},
cleanUp: {
type: "boolean",
demandOption: false,
describe: "Flag to delete stack on creation failure",
},
delete: {
type: "boolean",
demandOption: false,
describe: "Flag to Toggle Delete Stack",
},
});
};
export default async function initYargs(): Promise<yargs.Arguments> {
const params = configureYargs();
return params.argv;
}
Issue:
When I run the command with -hx
, yargs displays the help menu instead of an error. The -h
flag is meant to trigger help, and -x
is an alias for the secret
option. My expectation is that -hx
should result in an error, but it seems that -h
is being prioritized.
Question:
Why does -hx
show the help menu rather than an error? How can I configure yargs
to ensure that -hx
is treated as an error rather than a valid command?
Tried Adding below lines, Still not able fix the issue
.strict()
.strictOptions(true)
.strictCommands(true)