#๐Ÿ”’ from command line use argparse with named arguments as bool values

15 messages ยท Page 1 of 1 (latest)

neon zenith
#

I want to be able to --install or --uninstall and use those argument names but argparse to interpret them as bool values. In addition how would I best validate or limit the named arguments to only --install/--uninstall

What I don't want to do is --uninstall True or --uninstall False bool values should not be not specified by the user but assumed/default based on argument name

call python register.py --install 
call python register.py --uninstall
    parser = argparse.ArgumentParser(description="install or uninstall file")
    parser.add_argument("--install", action="store_true", help="Register file") # should be False
    parser.add_argument("--uninstall", action="store_true", help="Uninstall file") # should be true
    args = parser.parse_args()
    if not args.install and not args.uninstall: # better way to do this?
        parser.error("Please specify --register or --uninstall")
    if args.install and args.uninstall:
        parser.error("Please specify only one option --register or --Uninstall")

modify_ini(ini, "Global Clients", ".test", "testme", remove_key=args) # in modify_ini the remove_key argument adds or remove ini configurations
prisma domeBOT
#

@neon zenith

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

neon zenith
#

from command line use argparse with named arguments as bool values

winged tide
#

you can switch up the logical operators under demorgan's law

#
if not (args.install or args.uninstall):
neon zenith
#

Initially I thought having default python values for args would be pretty simple.

tame wigeon
#

default= in add_argument?

pallid rune
#

sounds like you want store_true and store_false with a shared dest= attribute, but i cant write/test an example atm

#

aight, now i can write one

#
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--install", action="store_true")
group.add_argument("--uninstall", action="store_false", dest="install")
args = parser.parse_args()
print(args)``` ```r
$ foo.py
usage: foo.py [-h] (--install | --uninstall)
foo.py: error: one of the arguments --install --uninstall is required
$ foo.py --install
Namespace(install=True)
$ foo.py --uninstall 
Namespace(install=False)```
pallid rune
neon zenith
#

!close

prisma domeBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.