The ask program prompts the user and accepts a single-key response.
The options can appear anywhere on the command line.
| Options | Description |
|---|---|
| -c, -C, –case-sensitive | Case-sensitive response matching |
| -h, -H, –help | Show this help screen |
The “prompt string” and responses are positional parameters. They must be in order.
| Positional Parameter | Description |
|---|---|
| “prompt string” | The prompt string the user sees |
| responses | The characters the user is allowed to press to answer the prompt (Not a comma-separated list.) |
| System Exit Code | Meaning |
|---|---|
| 0 | The user entered a response that was not in the allowed responses. |
| 1-125 | The index of the user’s choice in the responses. (The first response is 1, the next is 2, and so on.) |
| 126 | The user pressed Enter without making a choice |
| 127 | The user pressed Escape |
System Exit Code: In an sh-compatible shell, you check the ?* variable.
In a batch file you check the ERRORLEVEL.
Other shells may be different.
- The user must press Enter after pressing a key.
- The response is not case-sensitive by default. Use -c if case-sensitive mode is necessary.
- If the user presses more than one key, the first key will be used. The user can use the keyboard to edit their response.
- The escape sequences \, \a, \n, \r, and \t are allowed in the prompt string.
ask “** Answer [Y]es, [N]o, or [M]aybe: ” YNM
The example displays the following prompt, and reads the user’s response:
** Answer [Y]es, [N]o, or [M]aybe:
The example returns:
- Exit code 1 if the user pressed y or Y.
- Exit code 2 if the user pressed n or N.
- Exit code 3 if the user pressed m or M.
- Exit code 0 if the user pressed a key that was not y, Y, n, N, m, or M.
- Exit code 126 if the user pressed Enter without pressing a key.
- Exit code 127 if the user pressed Escape.
You can include the ask_funcs.h header and link the ask_funcs.o module to your own C/C++ programs. This provides your programs the same functionality used by the ask program.
It uses standard C library functions for I/O. It uses the standard putchar() function to print the prompt string to stdout, and the standard getchar() function to read the response character from stdin.
The ask_funcs module provides the following public functions:
void set_default_options();
void set_case_sensitive_mode(int value);
int ask(char *prompt, char *response_list);
void set_default_options();
- The
set_default_optionsfunction simply sets the default case_sensitive mode option to OFF (0).
void set_case_sensitive_mode(int value);
- The
set_case_sensitive_modefunction lets you turn the case sensitive mode ON (non-zero) or OFF (0).
int ask(char *prompt, char *responses);
- The
askfunction lets you prompt the user, specifying the prompt string and the response characters, and then receive the response code. - The return value from the
askfunction is the same as the system exit codes described for the ask program.
Leave a Reply