question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

using argparse with runscript?

See original GitHub issue

Is there a way to use argparse in combination with runscript? The only way I have found to pass arguments to my script from the command line is to do something like this:

python manage.py runscript myscript --script-args="1,a,banana"

Which is a little cryptic. What I’d like to be able to do is say:

python manage.py runscript myscript --number=1 --letter=a --fruit=banana

If I try to wrap up my args in quotes, like so:

python manage.py runscript myscript --script-args="--number=1 --letter=a --fruit=banana"

I get messages about “unknown parameter ‘number’”, which leads me to think that that’s being raised by the runscript command.

Any hints appreciated, or if there’s a good idea about how to implement this, I’d give it a try.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
ccurveycommented, Sep 5, 2017

I have not looked at this in a long time. IIRC, the underlying issue was that the “unrecognized arguments” error is raised by Django code, not by the django-extensions code. FWIW, I have started doing things this way:

def do_real_work(foo, bar):
    print foo, bar

def run(*args):
    """
    usage:  python manage.py runscript myscript --script-args="--foo=XXX --bar=YYYY"

    example:  python manage.py runscript myscript --script-args="--foo=something --bar=3"
    """
    import shlex
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("--foo", required=True)
    parser.add_argument("--bar", required=True)

    try:
        split_args = shlex.split(args[0])
    except IndexError:
        split_args = []

    args2 = parser.parse_args(split_args)

and then my invocation looks like

python manage.py runscript myscript --traceback --script_args="--foo=something --bar=3"
0reactions
trbscommented, Aug 27, 2018

closing due to inactivity

Read more comments on GitHub >

github_iconTop Results From Across the Web

Run python script with argparse in another script
I have a python script which uses some input parameters via argparse. something like this: **hello ...
Read more >
How To Use argparse to Write Command-Line Programs in ...
argparse allows you to call your own custom Python code with command-line arguments similar to how you might invoke git , ls ,...
Read more >
Build Command-Line Interfaces With Python's argparse
In this new implementation, you first import argparse and create an argument parser. To create the parser, you use the ArgumentParser class.
Read more >
python - structure a script with main, parse_args, subprocess
Run: ./script.py. Read in the R1.fastq read file. At the top of the script, import the modules argparse and sys; Define a function...
Read more >
Command Line Arguments in Python
Using sys.argv; Using getopt module; Using argparse module. Using sys.argv. The sys module provides functions and variables used to manipulate ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found