|
Limitations: getopts only handles short options. It does not
support The basic pattern is a |
|
|
Output:
With -v: Verbose: true Without flags: Verbose: false |
|
|
You can accept multiple flags by adding more letters to the optstring. Each letter becomes a valid option. Here |
|
|
Output:
With -v -d: Verbose: true, Debug: true With -v only: Verbose: true, Debug: false |
|
|
Users expect This example shows what happens when |
|
|
Output:
With -v (valid): Running with verbose=true With -h (not in optstring): Unknown option |
|
|
To support A here-document is a clean way to format multi-line help text. |
|
|
Output:
With -h flag: Usage: greet [-v] [-h] Options: -v Enable verbose output -h Show this help message Example: greet -v With -v flag: Running with verbose=true |
|
|
Some options need to accept a value, like
|
|
|
Output:
With -n Alice: Name: Alice With -n Bob: Name: Bob |
|
|
When an option takes an argument, you can attach the value
directly to the flag without a space. Both This is useful for compact command lines and is commonly
seen in tools like |
|
|
Output:
With space: -n Alice Name: Alice Attached: -nAlice Name: Alice Attached: -nBob Name: Bob |
|
|
You can mix flags and options with arguments. Each
|
|
|
Output:
With -v -n Alice -o results.txt: Verbose: true, Name: Alice, Output: results.txt With -n Bob only: Verbose: false, Name: Bob, Output: <none> |
|
|
When a user passes an unknown option (like It’s good practice to handle the |
|
|
Output:
With valid -v: Verbose: true With invalid -x (stderr suppressed for demo): Usage: script [-v] [-n name] |
|
|
Adding a leading In silent mode:
- |
|
|
Output:
With unknown option -x: Error: Unknown option -x With missing argument for -n: Error: -n requires an argument With valid -n Alice: Name: Alice |
|
|
Users can combine multiple flags into one argument.
Instead of This also works with options that take arguments:
|
|
|
Output:
Separate flags: -v -d Verbose: true, Debug: true, Name: <none> Combined flags: -vd Verbose: true, Debug: true, Name: <none> Combined with argument: -vdn Alice Verbose: true, Debug: true, Name: Alice |
|
|
After options, scripts often need positional arguments
(like filenames). The Use |
|
|
Output:
With -v file1.txt file2.txt: Verbose: true Files: file1.txt file2.txt With just file3.txt: Verbose: false Files: file3.txt |
|
|
Here’s a complete example combining all getopts concepts: flags, options with arguments, silent error handling, and positional arguments. This pattern is commonly used in production shell scripts. |
|
|
Output:
Basic usage: -v -o results.txt data.txt log.txt Verbose: true, Output: results.txt Processing: data.txt Processing: log.txt Help flag: -h Usage: process [-v] [-o outfile] file... |
|
|
A practical tar-like example: This pattern is common in Unix tools where multiple short options can be combined, with the last one optionally taking an argument. |
|
|
Output:
tar-like: -xvf archive.tar Extract: true, Verbose: true, File: archive.tar Separate flags: -x -v -f archive.tar Extract: true, Verbose: true, File: archive.tar |
|