Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

feat: handle mutually exclusive args in cli #732

Merged
merged 1 commit into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/chains/ethereum/options/src/database-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ export const DatabaseOptions: Definitions<DatabaseConfig> = {
normalize,
cliDescription: "Specify an alternative database instance, like MemDOWN",
disableInCLI: true,
legacyName: "db"
legacyName: "db",
conflicts: ["dbPath"]
},
dbPath: {
normalize,
cliDescription: "Specify a path to a directory to save the chain database.",
legacyName: "db_path",
cliAliases: ["db", "db_path"],
cliType: "string"
cliType: "string",
conflicts: ["db"]
}
};
15 changes: 10 additions & 5 deletions src/chains/ethereum/options/src/wallet-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ export const WalletOptions: Definitions<WalletConfig> = {
default: () => 10,
legacyName: "total_accounts",
cliAliases: ["a", "accounts"],
cliType: "number"
cliType: "number",
conflicts: ["accounts"]
},
accounts: {
normalize,
Expand All @@ -217,14 +218,16 @@ export const WalletOptions: Definitions<WalletConfig> = {
balance: BigInt(balance)
} as OptionsAccount;
});
}
},
conflicts: ["totalAccounts"]
},
deterministic: {
normalize,
cliDescription: "Use pre-defined, deterministic seed.",
default: () => false,
cliAliases: ["d", "deterministic"],
cliType: "boolean"
cliType: "boolean",
conflicts: ["mnemonic", "seed"]
},
seed: {
normalize,
Expand All @@ -240,7 +243,8 @@ export const WalletOptions: Definitions<WalletConfig> = {
"Random value, unless wallet.deterministic is specified",
legacyName: "seed",
cliAliases: ["s", "seed"],
cliType: "string"
cliType: "string",
conflicts: ["mnemonic", "deterministic"]
},
mnemonic: {
normalize,
Expand All @@ -254,7 +258,8 @@ export const WalletOptions: Definitions<WalletConfig> = {
defaultDescription: "Generated from wallet.seed",
legacyName: "mnemonic",
cliAliases: ["m", "mnemonic"],
cliType: "string"
cliType: "string",
conflicts: ["seed", "deterministic"]
},
unlockedAccounts: {
normalize,
Expand Down
6 changes: 6 additions & 0 deletions src/packages/cli/src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ function processOption(
coerce: optionObj.cliCoerce
};

if ("conflicts" in optionObj) {
options.conflicts = (optionObj as {
conflicts: string[];
}).conflicts.map(c => `${category}.${c}`);
}

const key = `${category}.${option}`;

// First, create *hidden* deprecated aliases...
Expand Down
30 changes: 21 additions & 9 deletions src/packages/options/src/definition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Base, CliTypeMap, CliTypes } from "./base";
import { ExclusiveGroupUnionAndUnconstrainedPlus } from "./exclusive";
import {
ExclusiveGroupsByName,
ExclusiveGroupUnionAndUnconstrainedPlus
} from "./exclusive";
import {
Legacy,
OptionCliType,
Expand Down Expand Up @@ -33,22 +36,31 @@ export type Definitions<C extends Base.Config> = {
readonly disableInCLI?: boolean;
readonly cliAliases?: string[];
readonly cliChoices?: string[] | number[];
} & (void extends OptionHasCliType<C, N>
? {
readonly cliType?: CliTypeMap<CliTypes> | null;
}
// exclusiveGroups (conflicts)
} & (C[ExclusiveGroupsByName<C, N>] extends never
? {}
: {
readonly cliType?: CliTypeMap<OptionCliType<C, N>> | null;
readonly cliCoerce?: (
cliType: OptionCliType<C, N>
) => OptionRawType<C, N>;
readonly conflicts: ExclusiveGroupsByName<C, N>[];
}) &
// cliType
(void extends OptionHasCliType<C, N>
? {
readonly cliType?: CliTypeMap<CliTypes> | null;
}
: {
readonly cliType?: CliTypeMap<OptionCliType<C, N>> | null;
readonly cliCoerce?: (
cliType: OptionCliType<C, N>
) => OptionRawType<C, N>;
}) &
// hasDefault
(void extends OptionHasDefault<C, N>
? {}
: {
readonly default: (config: InternalConfig<C>) => OptionType<C, N>;
readonly defaultDescription?: string;
}) &
// hasLegacy
(void extends OptionHasLegacy<C, N>
? {}
: {
Expand Down
14 changes: 14 additions & 0 deletions src/packages/options/src/exclusive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,20 @@ type Combine<
? I
: never;

export type ExclusiveGroupsByName<
C extends Base.Config,
N extends OptionName<C>,
GRPS extends ExclusiveGroups<C> = ExclusiveGroups<C>
> = GRPS extends [infer GRP, ...infer Rest]
? GRP extends unknown[]
? N extends DeepTupleToUnion<GRP>
? Exclude<DeepTupleToUnion<GRP>, N>
: Rest extends any[]
? ExclusiveGroupsByName<C, N, Rest>
: never
: never
: never;

type IsNeverType<T> = [T] extends [never] ? true : never;
export type ExclusiveGroupUnionAndUnconstrainedPlus<
C extends Base.Config,
Expand Down