Python Forum
How can I get some arguments using argparse?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I get some arguments using argparse?
#1
I am using a module argparse. I need to get arguments and then do something. I can only one argument now, but I cannot get some arguments if an user types two arguments.

python demo.py -c work.txt
Now here is my code:
import sys, argparse

def main():
    parser = argparse.ArgumentParser(description="Process some ...")
    #parser.add_argument('-n', '--name', required=True, help="name of the user")
    parser.add_argument('-n', '--name', required=False, help="name of the user")
    parser.add_argument('-a', '--adress', required=False, help="name of the host")
    parser.add_argument('-c', '--copy', required=False, help="Copy a file into a server")

    args = vars(parser.parse_args())
    #if args
    for x in args.values():
        if x != None:
            print(x)

main()
I can get them list_args like that:
python demo.py -a Russia -c work.txt
    args = vars(parser.parse_args())
    list_args = []
    for x in args.values():
        if x != None:
            list_args.append(x)

    print(list_args)
but then I cannot understand where who is.
How to do it?

OMG! I resolved it.
import argparse

def main():
    parser = argparse.ArgumentParser(description="Process some ...")

    parser.add_argument('-n', '--name', required=False, help="name of the user")
    parser.add_argument('-a', '--adress', required=False, help="name of the host")
    parser.add_argument('-c', '--copy', required=False, help="Copy a file to the server")

    args = vars(parser.parse_args())

    if args['name']:
        print('i am name {}'.format(args['name']))

    if args['adress']:
        print('i am adress {}'.format(args['adress']))

    if args['copy']:
        print('i am copy {}'.format(args['copy']))




main()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug argparse io issue pyDream 8 594 Apr-02-2024, 12:42 PM
Last Post: pyDream
  Not able to figure out what is wrong with argparse radioactive9 31 8,474 Mar-16-2022, 07:50 PM
Last Post: deanhystad
  argparse --help in one line. Denial 1 1,977 Sep-20-2020, 03:38 PM
Last Post: deanhystad
  Argparse error when inputting values tqader 2 2,870 Sep-11-2020, 07:42 PM
Last Post: buran
  Why this pycharm warning for argparse formatter_class value? pjfarley3 2 2,116 Sep-09-2020, 05:23 AM
Last Post: pjfarley3
  Can argparse support undocumented options? pjfarley3 3 2,183 Aug-14-2020, 06:13 AM
Last Post: pjfarley3
  In ArgParse, can two compatible formatter_class values be used? pjfarley3 2 2,576 Jul-31-2020, 02:01 PM
Last Post: pjfarley3
  Why am I getting KeyError 'file' when using argparse? Mike Ru 1 3,067 Jun-09-2019, 04:48 PM
Last Post: metulburr
  argparse 2skywalkers 4 3,114 May-15-2019, 07:00 PM
Last Post: Gribouillis
  argparse and imported modules spatialdawn 2 5,438 Dec-01-2018, 12:35 PM
Last Post: spatialdawn

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020