refactor: dedupe runtime and helper flows
This commit is contained in:
@@ -84,8 +84,16 @@ export function hasRootVersionAlias(argv: string[]): boolean {
|
||||
}
|
||||
|
||||
export function isRootVersionInvocation(argv: string[]): boolean {
|
||||
return isRootInvocationForFlags(argv, VERSION_FLAGS, { includeVersionAlias: true });
|
||||
}
|
||||
|
||||
function isRootInvocationForFlags(
|
||||
argv: string[],
|
||||
targetFlags: Set<string>,
|
||||
options?: { includeVersionAlias?: boolean },
|
||||
): boolean {
|
||||
const args = argv.slice(2);
|
||||
let hasVersion = false;
|
||||
let hasTarget = false;
|
||||
for (let i = 0; i < args.length; i += 1) {
|
||||
const arg = args[i];
|
||||
if (!arg) {
|
||||
@@ -94,8 +102,11 @@ export function isRootVersionInvocation(argv: string[]): boolean {
|
||||
if (arg === FLAG_TERMINATOR) {
|
||||
break;
|
||||
}
|
||||
if (arg === ROOT_VERSION_ALIAS_FLAG || VERSION_FLAGS.has(arg)) {
|
||||
hasVersion = true;
|
||||
if (
|
||||
targetFlags.has(arg) ||
|
||||
(options?.includeVersionAlias === true && arg === ROOT_VERSION_ALIAS_FLAG)
|
||||
) {
|
||||
hasTarget = true;
|
||||
continue;
|
||||
}
|
||||
if (ROOT_BOOLEAN_FLAGS.has(arg)) {
|
||||
@@ -111,46 +122,14 @@ export function isRootVersionInvocation(argv: string[]): boolean {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (arg.startsWith("-")) {
|
||||
return false;
|
||||
}
|
||||
// Unknown flags and subcommand-scoped help/version should fall back to Commander.
|
||||
return false;
|
||||
}
|
||||
return hasVersion;
|
||||
return hasTarget;
|
||||
}
|
||||
|
||||
export function isRootHelpInvocation(argv: string[]): boolean {
|
||||
const args = argv.slice(2);
|
||||
let hasHelp = false;
|
||||
for (let i = 0; i < args.length; i += 1) {
|
||||
const arg = args[i];
|
||||
if (!arg) {
|
||||
continue;
|
||||
}
|
||||
if (arg === FLAG_TERMINATOR) {
|
||||
break;
|
||||
}
|
||||
if (HELP_FLAGS.has(arg)) {
|
||||
hasHelp = true;
|
||||
continue;
|
||||
}
|
||||
if (ROOT_BOOLEAN_FLAGS.has(arg)) {
|
||||
continue;
|
||||
}
|
||||
if (arg.startsWith("--profile=") || arg.startsWith("--log-level=")) {
|
||||
continue;
|
||||
}
|
||||
if (ROOT_VALUE_FLAGS.has(arg)) {
|
||||
const next = args[i + 1];
|
||||
if (isValueToken(next)) {
|
||||
i += 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// Unknown flags and subcommand-scoped help should fall back to Commander.
|
||||
return false;
|
||||
}
|
||||
return hasHelp;
|
||||
return isRootInvocationForFlags(argv, HELP_FLAGS);
|
||||
}
|
||||
|
||||
export function getFlagValue(argv: string[], name: string): string | null | undefined {
|
||||
|
||||
Reference in New Issue
Block a user