Questions tagged [argparse]

A Python module for implementing command-line interfaces

argparse is a module for implementing command-line interfaces.

From the module documentation:

The argparse module makes it easy to write user-friendly command-line interfaces. The program defines what arguments it requires, and argparse will figure out how to parse those out of sys.argv. The argparse module also automatically generates help and usage messages and issues errors when users give the program invalid arguments.

argparse was added to the stdlib in 2.7/3.2, deprecating .

Resources:

3535 questions
1
vote
2 answers

Setting package data as default input for command line argument in a PyPI python package

I have written a command line script that requires a csv file included with my python package (layout of package below). I am using argparse on the command line script and am trying to make the default csv file the one that comes with the package.…
Cody Glickman
  • 514
  • 1
  • 8
  • 30
1
vote
0 answers

Ignore duplicates options in argparse

I'm using the choices argument in argparse. If I run -h I get the choices two times, once for -a and once for --argument. As I have around 30 options with long names the help text is quite cluttered. Can I only get it once? (without removing one of…
ErikXIII
  • 557
  • 2
  • 12
1
vote
0 answers

With argparse is it possible to have subparsers with dashes?

I want to specify "groups of arguments" like this: fetch-something --basic-auth --user=me --password=myP4ss OR fetch-something --oauth --user=me --client=9038534.client --secret=aslu432409x (similar to this question) I thought I could create…
frans
  • 8,868
  • 11
  • 58
  • 132
1
vote
0 answers

define complex mutual exclusive arguments with argparse

I know there is ArgumentParser.add_mutually_exclusive_group() which lets me choose between mutually exclusive options o1, o2, .. Can I also define 'complex' argument groups which are mutually exclusive? e.g. imagine I had to provide either username…
frans
  • 8,868
  • 11
  • 58
  • 132
1
vote
1 answer

Python argparse with subparsers and optional positional arguments

I would like to have a program with subparsers that handles specific arguments while also keep some positional and optional arguments to the previous parsers (In fact what I really want is only one option, I mean, a valid subparser OR a valid local…
Ralequi
  • 306
  • 2
  • 12
1
vote
2 answers

Store values of mutually exclusive options in same argument in Python argparse

I have a Python script that can deploy some software in three different environments, let's call them development, testing and production. In order to select which environment the script should work with, I want to use mutually exclusive flags, like…
José Tomás Tocino
  • 9,873
  • 5
  • 44
  • 78
1
vote
1 answer

Run python script with argparse in another script

I have a python script which uses some input parameters via argparse. something like this: **hello.py** import argparse parser = argparse.ArgumentParser() parser.add_argument("-n", "--name", required=True, help="name of the user") arguments =…
Catherine Nosova
  • 109
  • 1
  • 11
1
vote
1 answer

Autocomplete commands with argcomplete and Poetry

I'm currently trying Poetry to manage my dependencies, for a project using argparse and argcomplete. However, I can't figure out how to handle autocompletion with the run command of Poetry. I have a command mycommand which I can run with poetry run…
Michael Marx
  • 104
  • 8
1
vote
1 answer

Problem To Scraping Wikipedia Images Via Python

I wrote a Program With Python For Scraping First Image Link of a query in WikipediaSomethings Like This Image: My Python Program Require These Below Libraries: requests bs4 html re When I Run My Code then I give an argument it Returns a Defined…
1
vote
0 answers

Argparse argument value prefix with --

I find myself in the (unenviable) position of needing to pass a collection of arguments as a single (comma separated) argument to a script, like so: python example.py --extra_args '--argument1,value1,--argument2,--value2' It seems that argparse…
AS_Butler
  • 281
  • 2
  • 12
1
vote
0 answers

Script to Run Program and Feed it Input

I have a program, json_formatter.exe, that takes inputs of single filepaths in a command-line like environment and then formats the file it finds at the end of the path. I am trying to create a python script that runs the program and then feeds it…
Umbral Reaper
  • 325
  • 4
  • 9
1
vote
1 answer

Argparse + cmd2 RecursionError even on simple programs

Sample code: import argparse import cmd2 import sys def create_subparser(a_parser = None): if a_parser is None: new_parser = argparse.ArgumentParser() else: new_parser = a_parser.add_parser("single") …
wewlad
  • 11
  • 1
1
vote
1 answer

How to collect all possible arguments of an argparse.ArgumentParser

Given an argparse.ArgumentParser, e.g. parser.py from argparse import ArgumentParser p = ArgumentParser() subs = p.add_subparsers() a = subs.add_parser('a') a.add_argument('aa') b = subs.add_parser('b') b.add_argument('bb') p.parse_args() Now, I…
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
1
vote
1 answer

argparse - is it possible for user to enter argument not in one line?

I am new with this argparse module. import argparse parser = argparse.ArgumentParser(description='Input & Output Files') parser.add_argument('indir', type=str, help='Input File Path') parser.add_argument('outdir1', type=str, help='Output File Path…
qwe
  • 27
  • 4
1
vote
2 answers

argparse with 2 date arguments

I want to create a program which selects users from a database between 2 dates given on the command line. I have: import argparse parser = argparse.ArgumentParser() group =…
Frendom
  • 508
  • 6
  • 24