Python Forum
argument parser: to execute single function, and subsequent functions also
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
argument parser: to execute single function, and subsequent functions also
#4
I'm going to suggest a slightly different approach. How about if you allowed options such as -f 1 2 3? Meaning, run function_1(), then function_2(), followed by function_3().

This can be done with a single add_argument like this:
parser.add_argument("-f", nargs='+', default=[])
nargs='+' means any number of args, but at least 1. You can also use nargs='*' to mean any number, including 0. Including default=[] is nice because then your code doesn't have keep testing first if cmd_arguments.f == None.

The namespace object returned by parse_args() (cmd_arguments in your example) will have a member named f of type list. Now you can use if statements like:
if 1 in cmd_arguments.f:
    function_1()
This approach also gives great flexibility on not only which functions are to be run, but even what order: -f 3 1 2.
Reply


Messages In This Thread
RE: argument parser: to execute single function, and subsequent functions also - by dalwood - Feb-01-2018, 05:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Locally run an APK and execute functions using Python KovyJ 0 468 Jan-23-2025, 05:21 PM
Last Post: KovyJ
  mutable argument in function definition akbarza 1 1,234 Dec-15-2023, 02:00 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 2,371 Dec-25-2022, 03:00 PM
Last Post: askfriends
  i want to use type= as a function/method keyword argument Skaperen 9 3,674 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  Regex - Pass Flags as a function argument? muzikman 6 6,064 Sep-06-2021, 03:43 PM
Last Post: muzikman
  Picking a function to execute palladium 1 2,141 Feb-09-2021, 04:47 PM
Last Post: deanhystad
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 10,530 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  is there a single function to confine a number between two others? Skaperen 7 4,055 Nov-28-2020, 06:10 PM
Last Post: Skaperen
  How to use a tuple as an argument of a function zarox 5 9,149 Nov-14-2020, 08:02 PM
Last Post: buran
  calling a function and argument in an input phillup7 3 3,406 Oct-25-2020, 02:12 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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