Options
All
  • Public
  • Public/Protected
  • All
Menu

Cli Utils contains a set of static methods/helpers that are CLI related (forming options, censoring args, etc.)

export

Hierarchy

  • CliUtils

Index

Properties

Static CENSORED_OPTIONS

CENSORED_OPTIONS: string[] = ["auth", "p", "pass", "password", "passphrase", "credentials","authentication", "basic-auth", "basicAuth"]

A list of cli options/keywords that should normally be censored

static
memberof

CliUtils

Static Readonly CENSOR_RESPONSE

CENSOR_RESPONSE: "****" = "****"

Used as the place holder when censoring arguments in messages/command output

static
memberof

CliUtils

Methods

Static buildBaseArgs

  • Accepts the yargs argument object and constructs the base imperative argument object. The objects are identical to maintain compatibility with existing CLIs and plugins, but the intent is to eventually phase out having CLIs import anything from Yargs (types, etc).

    Parameters

    • args: Arguments

      Yargs argument object

    Returns ICommandArguments

    • Imperative argument object

Static censorCLIArgs

  • censorCLIArgs(args: string[]): string[]

Static censorYargsArguments

  • censorYargsArguments(args: Arguments): Arguments

Static extractArrayFromEnvValue

  • extractArrayFromEnvValue(envValue: string): string[]

Static extractEnvForOptions

  • Accepts the full set of command options and extracts their values from environment variables that are set.

    Parameters

    Returns ICommandArguments["args"]

    • the argument style object with both camel and kebab case keys for each option specified in environment variables.

Static formatHelpHeader

  • formatHelpHeader(header: string, indent?: string, color: string): string

Static getDashFormOfOption

  • getDashFormOfOption(optionName: string): string
  • Get the 'dash form' of an option as it would appear in a user's command, appending the proper number of dashes depending on the length of the option name

    Parameters

    • optionName: string

      e.g. my-option

    Returns string

    • e.g. --my-option

Static getEnvValForOption

  • getEnvValForOption(envPrefix: string, cmdOption: string): string | null
  • Get the value of an environment variable associated with the specified option name. The environment variable name will be formed by concatenating an environment name prefix, and the cmdOption using underscore as the delimiter.

    The cmdOption name can be specified in camelCase or in kabab-style. Regardless of the style, it will be converted to upper case. We replace dashes in Kabab-style values with underscores. We replace each uppercase character in a camelCase value with underscore and that character.

    The envPrefix will be used exactly as specified.

    Example: The values myEnv-Prefix and someOptionName would retrieve the value of an environment variable named myEnv-Prefix_SOME_OPTION_NAME

    memberof

    CliUtils

    Parameters

    • envPrefix: string

      The prefix for environment variables for this CLI. Our caller can use the value obtained by ImperativeConfig.instance.envVariablePrefix, which will use the envVariablePrefix from the Imperative config object, and will use the rootCommandName as a fallback value.

    • cmdOption: string

      The name of the option in either camelCase or kabab-style.

    Returns string | null

    • The value of the environment variable which corresponds to the supplied option for the supplied command. If no such environment variable exists we return null.

Static getOptValueFromProfiles

Static getOptionFormat

  • Takes a key and converts it to both camelCase and kebab-case.

    example
    Conversion of keys

    CliUtils.getOptionFormat("helloWorld");

    // returns const return1 = { key: "helloWorld", camelCase: "helloWorld", kebabCase: "hello-world" }

    /////////////////////////////////////////////////////

    CliUtils.getOptionFormat("hello-world");

    // returns const return2 = { key: "hello-world", camelCase: "helloWorld", kebabCase: "hello-world" }

    /////////////////////////////////////////////////////

    CliUtils.getOptionFormat("hello--------world");

    // returns const return3 = { key: "hello--------world", camelCase: "helloWorld", kebabCase: "hello-world" }

    /////////////////////////////////////////////////////

    CliUtils.getOptionFormat("hello-World-");

    // returns const return4 = { key: "hello-World-", camelCase: "helloWorld", kebabCase: "hello-world" }

    Parameters

    • key: string

      The key to transform

    Returns IOptionFormat

    An object that contains the new format.

Static getPositionalSyntaxString

  • getPositionalSyntaxString(positionalRequired: boolean, positionalName: string): string
  • Constructs the yargs style positional argument string.

    static
    memberof

    CliUtils

    Parameters

    • positionalRequired: boolean

      Indicates that this positional is required

    • positionalName: string

      The name of the positional

    Returns string

    • The yargs style positional argument string (e.g. );

Static mergeArguments

  • mergeArguments(...args: any[]): any
  • Using Object.assign(), merges objects in the order they appear in call. Object.assign() copies and overwrites existing properties in the target object, meaning property precedence is least to most (left to right).

    See details on Object.assign() for nuance.

    Parameters

    • Rest ...args: any[]

      variadic set of objects to be merged

    Returns any

    • the merged object

Static promptForInput

  • promptForInput(message: string): string
  • Display a prompt that hides user input and reads from stdin DOES NOT WORK WITH COMMANDS THAT ALSO READ STDIN Useful for prompting the user for sensitive info such as passwords Synchronous

    deprecated

    Use the asynchronous method readPrompt instead

    Parameters

    • message: string

      The message to display to the user e.g. "Please enter password:"

    Returns string

    value - the value entered by the user

Static promptWithTimeout

  • promptWithTimeout(questionText: string, hideText?: boolean, secToWait?: number): Promise<string>
  • Prompt the user with a question and wait for an answer, but only up to the specified timeout.

    deprecated

    Use readPrompt instead which supports more options

    example
     const answer = await CliUtils.promptWithTimeout("Type your answer here: ");
     if (answer === null) {
         // abort the operation that you wanted to perform
     } else {
         // use answer in some operation
     }
    

    Parameters

    • questionText: string

      The text with which we will prompt the user.

    • Default value hideText: boolean = false

      Should we hide the text. True = display stars. False = display text. Default = false.

    • Default value secToWait: number = 600

      The number of seconds that we will wait for an answer. If not supplied, the default is 600 seconds.

    Returns Promise<string>

    A string containing the user's answer, or null if we timeout.

Static readPrompt

  • Prompt the user with a question and wait for an answer, but only up to the specified timeout.

    example
     const answer = await CliUtils.readPrompt("Type your answer here: ");
     if (answer === null) {
         // abort the operation that you wanted to perform
     } else {
         // use answer in some operation
     }
    

    Parameters

    • message: string

      The text with which we will prompt the user.

    • Optional opts: IPromptOptions

    Returns Promise<string | null>

    A string containing the user's answer, or null if we timeout.

Static setOptionValue

  • setOptionValue(optName: string, optAliases: string[], value: any): ICommandArguments["args"]
  • Accepts an option name, and array of option aliases, and their value and returns the arguments style object.

    example
    Create Argument Object

    CliUtils.setOptionValue("my-option", ["mo", "o"], "value");

    // returns { "myOption": "value", "my-option": "value", "mo": "value", "o": "value" }

    Parameters

    • optName: string

      The command option name, usually in kebab case (or a single word)

    • optAliases: string[]

      An array of alias names for this option

    • value: any

      The value to assign to the argument

    Returns ICommandArguments["args"]

    • The argument style object

Static showMsgWhenDeprecated

Static sleep

  • sleep(timeInMs: number): Promise<unknown>

Generated using TypeDoc