Python. Argparser. Removing not-needed arguments

2024/10/12 6:23:39

I am parsing some command-line arguments, and most of them need to be passed to a method, but not all.

parser = argparse.ArgumentParser()
parser.add_argument("-d", "--dir", help = "Directory name", type = str, default = "backups")
parser.add_argument("-n", "--dbname", help = "Name of the database", type = str, default = "dmitrii")
parser.add_argument("-p", "--password", help = "Database password", type = str, default = "1123581321")
parser.add_argument("-u", "--user", help = "Database username", type = str, default = "Dmitriy")
parser.add_argument("-a", "--archive", help = "Archive backup", action="store_true")
args = parser.parse_args()backup(**vars(args)) # the method where i need to pass most of the arguments, except archive. Now it passes all.
Answer

Either create a new dictionary that does not have that key:

new_args = dict(k, v for k, v in args.items() if k != 'archive')

Or remove the key from your original dictionary:

archive_arg = args['archive'] # save for later
del args['archive'] #remove it
https://en.xdnf.cn/q/69682.html

Related Q&A

Where can I find some hello world-simple Beautiful Soup examples?

Id like to do a very simple replacement using Beautiful Soup. Lets say I want to visit all A tags in a page and append "?foo" to their href. Can someone post or link to an example of how to …

Difference between isnumeric and isdecimal in Python

What is the difference between isnumeric and isdecimal functions for strings ( https://www.tutorialspoint.com/python3/python_strings.htm )? They seem to give identical results: >>> "1234…

Estimating zip size/creation time

I need to create ZIP archives on demand, using either Python zipfile module or unix command line utilities. Resources to be zipped are often > 1GB and not necessarily compression-friendly.How do I e…

How to rename the index of a Dask Dataframe

How would I go about renaming the index on a dask dataframe? I tried it like sodf.index.name = foobut rechecking df.index.name shows it still being whatever it was previously.

Determine if a line segment intersects a polygon

If I have a vector (a line consisting of 2 points) on a 2D plane how can I determine if it has passed through a polygon? I know I can take each line which makes up the polygon and see if any intersect…

How to send a value from Arduino to Python and then use that value

I am in the process of building a robot that is remote controlled using Python to send control messages via the Internet through a simple GUI.I have gotten part of my code working pretty well, the GUI …

recaptcha wasnt solving by anticaptcha plugin in selenium python

Ive recently started using selenium for a project Ive been working on for a while that involves automation. One of the roadblocks in the plan was the ReCaptcha system, so I decided to use anti-captcha …

Python - check for class existance

Is there a way to check if a class has been defined/exists? I have different options on a menu, and some of them require inherited variables, so if you try to select that function before you have set …

getting a matplotlib colorbar tick outside data limits for use with boundaries keyword

I am trying to use a colorbar to label discrete, coded values plotted using imshow. I can achieve the colorbar that I want using the boundaries and values keywords, which makes the maximum value of the…

Apache Airflow - customize logging format

Is it possible to customize the format that Airflow uses for logging?I tried adding a LOG_FORMAT variable in $AIRFLOW_HOME/airflow.cfg, but it doesnt seem to take effectLOG_FORMAT = "%(asctime)s …