Using the Choice Command (with Examples)

Using the Choice Command (with Examples)

The Choice command in Windows allows users to prompt for user input and return the selected choice index. This command is particularly useful when creating batch scripts or automating certain tasks that require user interaction.

Example 1: Prompting for a Yes or No choice

choice

Motivation: This command can be used when we want the user to make a simple yes or no decision. It provides a prompt with the default options of “Y” for Yes and “N” for No. By default, the selected choice index is returned.

Explanation:

  • The choice command prompts the user with the available choices and waits for the user to make a selection.
  • The user can then press either “Y” or “N” and press Enter to select their choice.
  • The selected choice index is then returned by the command.

Example Output:

(Y/N)?

Example 2: Prompting for a choice from a specific set

choice /c AB

Motivation: This command is useful when we want to present the user with a specific set of choices other than just “Y” or “N”. It allows us to define our own set of choices for the user to select from.

Explanation:

  • The /c option is used to specify the available choices as a string. In this example, we use “AB” to set the options as “A” and “B”.
  • The choice command will then display the available choices to the user.
  • The user can press either “A” or “B” and press Enter to select their choice.
  • The selected choice index (1 for “A”, 2 for “B”) is returned by the command.

Example Output:

(A,B)?

Example 3: Prompting for a choice with a specific message

choice /m "message"

Motivation: This command is useful when we want to display a custom message along with the available choices to the user. It allows us to provide additional information or instructions to the user.

Explanation:

  • The /m option is used to specify the message to be displayed to the user. In this example, we use “message” as a placeholder for the desired message.
  • The choice command will then display the custom message along with the available choices.
  • The user can make a selection by entering the corresponding choice and pressing Enter.
  • The selected choice index is returned by the command.

Example Output:

message (1,2)?

Example 4: Prompting for a case-sensitive choice from a specific set

choice /cs /c Ab

Motivation: This command is helpful when we want to enforce case sensitivity for the choices. It allows us to differentiate between uppercase and lowercase options.

Explanation:

  • The /cs option is used to indicate that the choice selection should be case-sensitive.
  • The /c option is used to specify the available choices as a string. In this example, we use “Ab” to set the options as “A” and “b”.
  • The choice command will then display the available choices to the user, distinguishing between uppercase and lowercase.
  • The user can select either “A” or “b” by entering the corresponding choice and pressing Enter.
  • The selected choice index (1 for “A”, 2 for “b”) is returned by the command.

Example Output:

(A,b)?

Example 5: Prompting for a choice and preferring the default choice within a specific time

choice /t 5 /d N

Motivation: This command is useful when we want to automatically select a default choice if the user does not make a selection within a specified time. It ensures that the script can continue executing even if there is no user input.

Explanation:

  • The /t option is used to specify the time in seconds. In this example, we use 5 to set the timeout period to 5 seconds.
  • The /d option is used to specify the default choice. In this example, we use “N” as the default choice.
  • The choice command will display the available choices to the user along with the default choice.
  • If the user does not make a selection within the specified time, the default choice is automatically selected.
  • The selected choice index (1 for default choice) is returned by the command.

Example Output:

(N)?

Example 6: Displaying help

choice /?

Motivation: This command can be used when we need to quickly access the documentation or help information for the choice command. It provides a summary of the available options and their usage.

Explanation:

  • The choice /? command displays the help information for the choice command.
  • It provides a brief explanation of the different options available, their usage, and any additional notes or examples.

Example Output:

Prompt user to select a choice and return the selected choice index.

CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text] [/?]

   /C    choices       Specifies the set of choices to be created.
                       Default list is "YN".

   /N                  Hides the list of choices in the prompt.
                       The message before the prompt is displayed
                       and the choices are still enabled.

   /CS                 Enables case-sensitive choices to be selected.
                       By default, the utility is case-insensitive.

   /T    timeout       The number of seconds to pause before a default
                       choice is made. Acceptable values are from 0 to
                       9999. If 0, no default choice is made.

   /D    choice        Specifies the default choice after nnnn seconds.
                       Character must be in the set of choices specified
                       by /C option and must also specify nnnn with the
                       /T option.

   /M    text          Specifies the message to be displayed before
                       the prompt. If not specified, the utility
                       displays only a prompt.

   /?                  Displays this help message.

By utilizing these different options of the choice command, users can create more interactive batch scripts and automate tasks that require user input. The command provides flexibility in presenting options to users and allows for a seamless flow of user-driven interactions within the scripts or automation processes.

Related Posts

How to use the command jhsdb (with examples)

How to use the command jhsdb (with examples)

Jhsdb is a command-line tool that is used to attach to a Java process or launch a postmortem debugger to analyze the core dump from a crashed Java Virtual Machine.

Read More
How to use the command runit (with examples)

How to use the command runit (with examples)

Runit is a 3-stage init system, which is responsible for starting and managing processes on Unix-like systems.

Read More
How to use the command ttyplot (with examples)

How to use the command ttyplot (with examples)

This article will provide examples and explanations of various use cases for the ttyplot command, a real-time plotting utility for the command-line with data input from stdin.

Read More