Python Forum
How to reuse positional arguments entered in terminal.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to reuse positional arguments entered in terminal.
#1
Hello Guys, quick question from a beginner. I am learning about the module argparse and I wanted to know about this code. You see the positional arguments there -a and -b , these are storing values right. Suppose, I type on the terminal -a 42 -b 36, is there are way to extract those variables from the terminal to my program so that I can use those numbers for some functions. For e.g I might want to use the numbers entered 42 and 36 to be used in a function called sum where I add these two numbers. Basically, how do I capture their values entered because a, b are not actual variables that I can use.


import argparse

import os
import sys


if __name__=="__main__":
    # Create the parser
    my_parser = argparse.ArgumentParser(prog='parsing',
                                        description='List the content of a folder',
                                        epilog='Enjoy the program :)')

    # Add the arguments
    my_parser.add_argument('Path',
                           metavar='path',
                           type=str,
                           help='the path to list')
    my_parser.add_argument('-a', action='store', help='This is 1st arg')
    my_parser.add_argument('-b', action='store', help='This is 2nd arg')
    my_parser.add_argument('-l',
                           '--long',
                           action='store_true',
                           help='enable the long listing format')

    # Execute the parse_args() method
    args = my_parser.parse_args()
    print("All parameters have been read")
    input_path = args.Path

    if not os.path.isdir(input_path):
        print('The path specified does not exist')
        sys.exit()

    for line in os.listdir(input_path):
        if args.long: #simplified long listing
            size = os.stat(os.path.join(input_path, line)).st_size
            line = '%10d %s' % (size, line)
        print(line)
Reply
#2
On line 26, all the parsed arguments are returned into args as a Namespace Object.

Since you didn't override the destination, the -a argument will go into the "a" attribute. So you can get the information as args.a, just like you're getting the path as args.Path
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,858 Oct-17-2022, 06:29 PM
Last Post: paulo79
  TypeError: missing 3 required positional arguments: wardancer84 9 10,665 Aug-19-2021, 04:27 PM
Last Post: deanhystad
  Try,Except,Else to check that user has entered either y or n (Code block pasted) RandomNameGenerator 3 2,302 Jun-29-2021, 08:21 PM
Last Post: RandomNameGenerator
  TypeError: max_value() missing 2 required positional arguments: 'alpha' and 'beta' Anldra12 2 4,168 May-15-2021, 04:15 PM
Last Post: Anldra12
  Please help me to ask to enter again itself when value entered is not a number. sunil422 5 2,540 Aug-13-2020, 02:15 PM
Last Post: perfringo
  random.choice() takes two positional arguments, but three were given. ShakeyPakey 5 11,491 May-31-2020, 03:13 PM
Last Post: deanhystad
  how to divide one big equation entered as a string to small chunks gungurbuz 1 1,596 May-28-2020, 03:46 PM
Last Post: Larz60+
  TypeError: add() missing 2 required positional arguments NectDz 5 12,962 May-28-2020, 02:54 PM
Last Post: BitPythoner
  add() takes 2 positional arguments but 3 were given Man_from_India 3 5,699 Feb-10-2020, 05:08 PM
Last Post: Man_from_India
  How to check if user entered string or integer or float?? prateek3 5 11,284 Dec-21-2019, 06:24 PM
Last Post: DreamingInsanity

Forum Jump:

User Panel Messages

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