shraddha maurya
shraddha maurya

Reputation: 111

Converting the command line python code to normal python script without command line

I have the following code which takes input from the command line. I want to run this code in streamlit and not take the values of the arguments from the command line, instead set them to some default values like for '-i', I want it by default to open the camera. How can I achieve this?

def build_argparser():
    parser = ArgumentParser()

    general = parser.add_argument_group('General')
    general.add_argument('-i', '--input', metavar="PATH", default='0',
                         help="(optional) Path to the input video " \
                         "('0' for the camera, default)")
    general.add_argument('-o', '--output', metavar="PATH", default="",
                         help="(optional) Path to save the output video to")
    general.add_argument('--no_show', action='store_true',
                         help="(optional) Do not display output")
    general.add_argument('-tl', '--timelapse', action='store_true',
                         help="(optional) Auto-pause after each frame")
    general.add_argument('-cw', '--crop_width', default=0, type=int,
                         help="(optional) Crop the input stream to this width " \
                         "(default: no crop). Both -cw and -ch parameters " \
                         "should be specified to use crop.")
    general.add_argument('-ch', '--crop_height', default=0, type=int,
                         help="(optional) Crop the input stream to this height " \
                         "(default: no crop). Both -cw and -ch parameters " \
                         "should be specified to use crop.")
    general.add_argument('--match_algo', default='HUNGARIAN', choices=MATCH_ALGO,
                         help="(optional)algorithm for face matching(default: %(default)s)")

Upvotes: 1

Views: 785

Answers (1)

adrtam
adrtam

Reputation: 7221

See: https://docs.python.org/3/library/argparse.html#the-parse-args-method

Normally what we do is this:

parser = argparse.ArgumentParser()
# ... define what to expect
arg = parser.parse_args()

and arg will be the argument object, which is parsed from sys.argv, which is what you entered in command line. You can also put the list of strings into the function, such as

arg = parser.parse_args(["--match_algo", "-ch"])

The link above have more examples for different variations of the arguments you may use.

Upvotes: 2

Related Questions