-2

I use argparse to specify multiple command line arguments. How would I run a particular function that is associated with an argparse command line input?

Consider: py test.py -s google.com

As the argument -s has been entered, a specific function only associated with the -s argument should be ran.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Mutlithrd
  • 21
  • 4
  • I know that I can achieve this by using the input function to store the entered text, test the relevant condition and call the associated function call that way. However I explicitly only want to do this using argparse. – Mutlithrd Mar 04 '23 at 01:59
  • 1
    Do you know how to use argparse to parse the `-s` argument? (if not, do you know how to use argparse *at all*?) Do you understand what you will get back from argparse after using it? Do you understand how to check whether the argument is present? Do you understand how to run the function? If you put those things together, does it solve the problem? **Where exactly are you stuck?** – Karl Knechtel Mar 04 '23 at 02:00
  • Hi @KarlKnechtel I only want to capture the relevant switch/argument that was entered i.e. '-s' and perform the arguments associated function call. '-s' will call a different function than '-w'. Pseudo code: if argparse argument that is entered is == '-w' call wfunction() else call sfunction() – Mutlithrd Mar 04 '23 at 02:02
  • 1
    `argparse` is primarily a parser - used to figure out what your user wants (including multiple options). It is not an 'executer' - that's normally done by your own code, based on the parsed arguments. The docs has an example under `subcommands` on how you can streamline the execution. There are other parsers (e.g. `plac`) that do more to integrate parsing and function calls. – hpaulj Mar 04 '23 at 02:11
  • argparse has been in Python for quite some time. – mkrieger1 Mar 04 '23 at 02:57
  • `argparse` questions are best when they include actual `argparse` code - including `add_argument` and `parse_args` input/results. Focus for a start on the parsing. As the duplicates show, there are many ways to match the input strings with actual functions. For a beginner I think the obvious set of `if/else` statements after parsing is a good start. Worry more about being clear than about being "efficient" or clever. – hpaulj Mar 04 '23 at 18:16

1 Answers1

0

Here is a very simple example of using argparse options to trigger specific functions.

After adding the -w and -s as arguments, you can test the returned Namespace object to see which arguments were found and call the appropriate functions accordingly.

main.py

import argparse


def sfunc():
    print("-s argument was found")

def wfunc():
    print("-w argument was found")


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("-w", action="store_true")
    parser.add_argument("-s", action="store_true")
    ns = parser.parse_args()
    if ns.s:
        sfunc()
    if ns.w:
        wfunc()

Here is what the results are from using each of the arguments

/>python main.py -s
-s argument was found

/>python main.py -w
-w argument was found

/>python main.py -w -s
-s argument was found
-w argument was found

There are many other ways to achieve these same results, this is just a very simple example.

Alexander
  • 16,091
  • 5
  • 13
  • 29