argparse

Generate json schema from argparse CLI

Generate json schema from argparse CLI Question: I have a CLI written with argparse and I was wondering if there was a way to produce a JSON schema from the ArgumentParser? The thought behind this being to distribute the JSON schema to extensions interfacing with the application, thus removing the need for each extension to …

Total answers: 1

Custom conflict handling for ArgumentParser

Custom conflict handling for ArgumentParser Question: What I need I need an ArgumentParser, with a conflict handling scheme, that resolves some registered set of duplicate arguments, but raises on all other arguments. What I tried My initial approach (see also the code example at the bottom) was to subclass ArgumentParser, add a _handle_conflict_custom method, and …

Total answers: 4

How to see the attributes of argparse.NameSpace Python object?

How to see the attributes of argparse.NameSpace Python object? Question: For a project involving multiple scripts (data processing, model tuning, model training, testing, etc.) I may keep all my <class ‘argparse.ArgumentParser’> objects as the return value of functions for each script task in a module I name cli.py. However, in the calling script itself (__main__), …

Total answers: 2

Parsing a string using argparse

Parsing a string using argparse Question: So I want the input of argparse to be a string instead of the command line. for example: python3 some_script.py arg1 arg2 arg3 I want to give the argparse the string "arg1 arg2 arg3" import argparse command = "arg1 arg2 arg3" parser = argparse.ArgumentParser() # add args here args …

Total answers: 1

python argparse in a docker container based on ubuntu

python argparse in a docker container based on ubuntu Question: I am struggling to find a way to pass arguments to a python script within a docker container based on ubuntu. I am working with docker-compose.yml. Please find the example below! docker-compose.yml version: "3" services: bcp: image: ubuntu:18.04 restart: always tty: true entrypoint: ["/bin/bash", "/ingestion/bcp-entrypoint.sh"] …

Total answers: 2

Suppressing option processing after a non-option command-line argument

Suppressing option processing after a non-option command-line argument Question: I am writing a wrapper script using argparse which accepts a list of arguments that are going to be passed to another program, and some options that are specific to my wrapper. To avoid confusion regarding which options go where, I want any appearance of a …

Total answers: 1

Positional argument after optional argument list

Positional argument after optional argument list Question: I’m somewhat new to using argparse and have run into a problem stemming from using an optional argument (list of files arbitrarily long) along with a positional argument (a single file). Here is a simple program that demonstrates this problem: parser = argparse.ArgumentParser() parser.add_argument(“pos_file”, help=”Input file”) parser.add_argument(“-v”, “–verbose”, …

Total answers: 2

How to specify a minimum or maximum float value with argparse

How to specify a minimum or maximum float value with argparse Question: How can I specify a minimum or maximum floating point argument using argprase? I’d like to be able to provide a command-line argument between a min and max floating point value. The closest thing I can find is the choices option in add_argument(), …

Total answers: 4

Getting full path from the relative path with Argparse

Getting full path from the relative path with Argparse Question: I am writing a small command line utility for my script and I can’t figure out how to properly get the full path from the argument. Here’s what I mean. parser = ArgumentParser() parser.add_argument(“-src”, required = True, help=”path to some folder”) args = parser.parse_args() print(args.src) …

Total answers: 3

Argparse optional boolean

Argparse optional boolean Question: I am trying to get the following behaviour: python test.py ⟹ store foo=False python test.py –foo ⟹ store foo=True python test.py –foo bool ⟹ store foo=bool It works when I use parser.add_argument(‘–foo’, nargs=’?’, default=False, const=True) However, it breaks if I add type=bool, trying to enforce casting to boolean. In this case …

Total answers: 2