2

I'm trying to pass arguments in a Flask application with argparse:

app = Flask(__name__)

parser = argparse.ArgumentParser()
parser.add_argument("-e", "--environ", dest='environ', default='production',
                    help="Server environment")
args = parser.parse_args()

if args.environ == 'dev':
    app.config.from_pyfile("dev.cfg", silent=True)
else:
    app.config.from_pyfile("product.cfg", silent=True)

Everything is OK when I run the script directly. However I don't know how to pass "-e dev" argument in uwsgi config file, pyargv cannot handle this kind of argument.

Vayn
  • 2,507
  • 4
  • 27
  • 33
  • 1
    pyargv simply populate the sys.argv list. So you can use it as argparse (as all of the pther arg parsers) read that list – roberto Nov 16 '14 at 05:25
  • You're right! I use positional argument instead of keyword argument and uwsgi works fine now. And I still want to know there is any way to pass keyword argument in uwsgi. – Vayn Nov 16 '14 at 08:43
  • 2
    there is no such thing as keyword arguments. The system passes arguments as a dumb array that the python engine maps to sys.argv list. argparse only parses this list by conventions. --payargv "-e foobar" is perfectly valid. – roberto Nov 16 '14 at 09:25
  • You can use `pyargv=-e dev` in a .ini file. See my answer at https://stackoverflow.com/a/46702137/1410035 for more detail – Tom Saleeba Oct 12 '17 at 05:38

1 Answers1

-2

You can use this code:

parser.add_argument("-e", "--environ", dest='environ', default='production', help="Server environment", required=False)
Dondu
  • 1
  • 1