Python Forum
How do I call sys.argv list inside a function, from the CLI?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I call sys.argv list inside a function, from the CLI?
#4
Better use the arparse module from the standard library to handle CLI arguments
import argparse

class Hardware:
    pass
class Spam:
    pass
class Eggs:
    pass

if __name__ == '__main__':
    functions = {'Hardware': Hardware,
                 'Spam': Spam,
                 'Eggs': Eggs}

    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--connect', '-c',
        choices=functions.values(),
        nargs='*',
        action='extend',
        dest='inlist',
        type=functions.get)
    ns = parser.parse_args()
    print(ns.inlist)
    for f in ns.inlist:
        f()
Output:
$ python paillasse/pf/billy.py -c Hardware Eggs [<class '__main__.Hardware'>, <class '__main__.Eggs'>]
Reply


Messages In This Thread
RE: How do I call sys.argv list inside a function, from the CLI? - by Gribouillis - May-02-2023, 08:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  for loops break when I call the list I'm looping through Radical 4 1,058 Sep-18-2023, 07:52 AM
Last Post: buran
  with open context inside of a recursive function billykid999 1 680 May-23-2023, 02:37 AM
Last Post: deanhystad
  sys.argv method nngokturk 3 1,268 Jan-23-2023, 10:41 PM
Last Post: deanhystad
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,297 Jan-09-2022, 02:39 AM
Last Post: Python84
  how to call an object in another function in Maya bstout 0 2,188 Apr-05-2021, 07:12 PM
Last Post: bstout
  import argv Scott 3 6,071 Apr-01-2021, 11:03 AM
Last Post: radix018
  In this function y initially has no value, but a call to foo() gives no error. Why? Pedroski55 8 3,763 Dec-19-2020, 07:30 AM
Last Post: ndc85430
  How to make global list inside function CHANKC 6 3,306 Nov-26-2020, 08:05 AM
Last Post: CHANKC
  How to create a linked list and call it? loves 12 4,942 Nov-22-2020, 03:50 PM
Last Post: loves
  Struggling for the past hour to define function and call it back godlyredwall 2 2,346 Oct-29-2020, 02:45 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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