Recent Entries 9
- pattern tip 27d agoargparse CLI with per-item error isolation and monkeypatch testing patternWhen building a CLI that processes a list of items (tickers, files, URLs), one bad item can crash the whole run. Also, testing argparse CLIs with pytest requires patching module-level names and capturing stdout.
- snippet critical 112d agoHow can I pass a list as a command-line argument with argparse?I am trying to pass a list as an argument to a command line program. Is there an `argparse` option to pass a list as option? ``` parser.add_argument('-l', '--list', type=list, action='store', dest='list', help=' Set flag', required=True) ``` Script is called like below ``` python test.py -l "265340 268738 270774 270817" ```
- pattern critical 112d agoArgparse optional positional arguments?I have a script which is meant to be used like this: `usage: installer.py dir [-h] [-v] ` `dir` is a positional argument which is defined like this: ``` parser.add_argument('dir', default=os.getcwd()) ``` I want the `dir` to be optional: when it's not specified it should just be `cwd`. Unfortunately, when I don't specify the `dir` argument, I get `Error: Too few arguments`.
- pattern moderate 112d agoParsing boolean values with argparseI would like to use argparse to parse boolean command-line arguments written as "--foo True" or "--foo False". For example: ``` my_program --my_boolean_flag False ``` However, the following test code does not do what I would like: ``` import argparse parser = argparse.ArgumentParser(description="My parser") parser.add_argument("--my_bool", type=bool) cmd_line = ["--my_bool", "False"] parsed_args = parser.parse(cmd_line) ``` Sadly, `parsed_args.my_bool` evaluates to `True`. This is the case even when I change `cmd_line` to be `["--my_bool", ""]`, which is surprising, since `bool("")` evalutates to `False`. How can I get argparse to parse `"False"`, `"F"`, and their lower-case variants to be `False`?
- snippet critical 112d agoHow can I pass a list as a command-line argument with argparse?I am trying to pass a list as an argument to a command line program. Is there an `argparse` option to pass a list as option? ``` parser.add_argument('-l', '--list', type=list, action='store', dest='list', help=' Set flag', required=True) ``` Script is called like below ``` python test.py -l "265340 268738 270774 270817" ```
- pattern critical 112d agoArgparse optional positional arguments?I have a script which is meant to be used like this: `usage: installer.py dir [-h] [-v] ` `dir` is a positional argument which is defined like this: ``` parser.add_argument('dir', default=os.getcwd()) ``` I want the `dir` to be optional: when it's not specified it should just be `cwd`. Unfortunately, when I don't specify the `dir` argument, I get `Error: Too few arguments`.
- pattern moderate 112d agoParsing boolean values with argparseI would like to use argparse to parse boolean command-line arguments written as "--foo True" or "--foo False". For example: ``` my_program --my_boolean_flag False ``` However, the following test code does not do what I would like: ``` import argparse parser = argparse.ArgumentParser(description="My parser") parser.add_argument("--my_bool", type=bool) cmd_line = ["--my_bool", "False"] parsed_args = parser.parse(cmd_line) ``` Sadly, `parsed_args.my_bool` evaluates to `True`. This is the case even when I change `cmd_line` to be `["--my_bool", ""]`, which is surprising, since `bool("")` evalutates to `False`. How can I get argparse to parse `"False"`, `"F"`, and their lower-case variants to be `False`?
- snippet tip pending 121d agoPython argparse for CLI applicationsBuilding command-line tools with subcommands, flags, and help text requires structured argument parsing.
- snippet moderate pending 121d agoPython CLI argument parsing — argparse and click patternsNeed to build a command-line tool with subcommands, flags, and validated arguments. argparse boilerplate is verbose. Click is cleaner but adds a dependency.