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
1 answer

Accessing Python CLI arguments without dot

I'm using argparse for my Python CLI as follows: import argparse parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument('--myarg', type=str, help="Dummy arg") args = parser.parse_args() #…
stefanbschneider
  • 5,460
  • 8
  • 50
  • 88
1
vote
4 answers

python argparse - How to prevent by using a combination of arguments

In argparse, I want to prevent a particular combination of arguments. Lets see the sample code. Sample: import argparse parser = argparse.ArgumentParser(add_help=False) parser.add_argument('--firstname', dest='fn',…
TheDataGuy
  • 2,712
  • 6
  • 37
  • 89
1
vote
1 answer

Separate parserarg input YYYY MM and DD into separate variables then concatenate as a string and post

Note: User is to respond with YYYY-MM-DD format in Python: Import argparse. Saved this value to "data" and "post(data)". SetUp: Import requests, json, dotenv, sys, os, argparse, datetime, calendar from datetime import datetime from time import…
1
vote
1 answer

Nargs='+' capturing too many arguments python

I am using argparse for command line arguments, where one argument is one or more .csv files: parser = argparse.ArgumentParser(description='...') parser.add_argument('csv', type=str, nargs='+', help='.csv file(s)')) parser.add_argument('days',…
user11035198
1
vote
1 answer

How to avoid argparser type error when omitting argument?

Using argparser in R, I'm getting an error when specifying the type of an argument in the call to add_argument but not passing an argument to the script at the MacOSX command line. For example, given this R script: library(argparser) p <-…
tef2128
  • 740
  • 1
  • 8
  • 19
1
vote
0 answers

Implementing master commands with python argparse to shorten CLI calls

I am searching for a way to implement master commands for my python script. By master commands i mean one command summarizing a sequence of commands. An example call with all commands written out: python script.py -import model.glb…
Mamu
  • 21
  • 2
1
vote
1 answer

Spoof argparse in Jupyter Notebook

I am trying to use a function that was written with argparse in a Jupyter Notebook. To do this I'm trying to "spoof" the argparse, but can't figure out how to do it. I found this useful SO question/answer but I'm still understanding something about…
jss367
  • 4,759
  • 14
  • 54
  • 76
1
vote
0 answers

How to make conditional arguments using argparse?

I have a file named test.py. There are 3 ways to use it: You can load the contents of a json file, and it will generate graphs using that data (eg: python test.py -l './data.json') You can give it a YAML file, and it will use some information in it…
Kadhir
  • 143
  • 1
  • 2
  • 12
1
vote
2 answers

Support global arguments before or after sub-command in argparse

Setting the parents argument with a parser will allow for sharing common arguments between parsers (e.g. parents and sub-commands). But, applying a base parser to both the parent and sub-command appears to overwrite the value from the parent parser…
palswim
  • 11,856
  • 6
  • 53
  • 77
1
vote
1 answer

parse "mount style" options with argparse

What is the best option to parse "mount style" options in python? I need to parse argument=value lists associated with a specific argument, as in the following example: myprogram --database type=mysql,user=root,passwd=secret,database=mydb --mail…
ZioByte
  • 2,690
  • 1
  • 32
  • 68
1
vote
1 answer

Understanding argparse to get dynamic maps with Geo-Location of tweets

I have found this python code online (twitter_map_clustered.py) which (I think) help create a map using the geodata of different tweets.: from argparse import ArgumentParser import folium from folium.plugins import MarkerCluster import json def…
bravopapa
  • 425
  • 4
  • 13
1
vote
1 answer

Argparse using subcommands/subparsers -- AttributeError: 'Namespace' object has no attribute

I need to use a python script having the following usages: script.py ( commands ) ( options ) My problem is how i add arguments for "commands" and "options"? What i did now is this: parser = argparse.ArgumentParser() parser._optionals.title =…
happymatei
  • 113
  • 1
  • 12
1
vote
2 answers

Using argparse to open a json file?

I recently just started using argparse and I'm trying to use it to open a json file among other things like outputting into a csv file. import argparse import os import json import csv import pandas as pd parser =…
1
vote
1 answer

Argparse error: Too few arguments on python2.x

I am trying to use the argparse library with python2 but it always gives error: Code: parent_parser = argparse.ArgumentParser() sub_parsers = parent_parser.add_subparsers(title = "actions") parser_create = sub_parsers.add_parser("create", parents =…
Omar Abdelrazik
  • 683
  • 2
  • 9
  • 30
1
vote
2 answers

Python argparse argument order in help text

My Python program accepts command line arguments using the argparse module and it works as intended, however, the help text is a bit misleading and I would like to fix it for others using my program. Currently, I have a positional argument which is…
1 2 3
99
100