Skip to content

Commit 4c940e1

Browse files
committed
Add reading options from file illustration
1 parent 1ad00c0 commit 4c940e1

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

source-code/command-line-arguments/ArgParse/README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ $ ./generate_gaussian.py -h
1414
1. `partial_parse.py`: illustrate how to parse only known arguments.
1515
1. `two_stage_parse.py`: parsing input from a file as if it were command
1616
line arguments (using `shlex`) and merging with command line arguments.
17-
```bash
18-
$ ./two_stage_parse.py -l mem=8gb -I job_script.pbs -k oe
19-
```
20-
`. `job_script.pbs`: file to use with `two_stage_parse.py`.
17+
```bash
18+
$ ./two_stage_parse.py -l mem=8gb -I job_script.pbs -k oe
19+
```
20+
1. `job_script.pbs`: file to use with `two_stage_parse.py`.
21+
1. `options_in_file.py`: illustration of how to read options from a file.
22+
```bash
23+
./options_in_file.py --foo something @file_options.txt
24+
```
25+
1. `file_options.txt`: file containing options for `options_in_file.py`.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--foo
2+
This is the foo value
3+
--flag
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
5+
6+
def amin():
7+
arg_parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
8+
arg_parser.add_argument('--foo', help='foo option')
9+
arg_parser.add_argument('--bar', help='bar optoin')
10+
arg_parser.add_argument('--flag', action='store_true',
11+
help='flag option')
12+
options = arg_parser.parse_args()
13+
print(options)
14+
15+
if __name__ == '__main__':
16+
amin()

0 commit comments

Comments
 (0)