4

I have built a command line python script using flask-script package to access an sql database that a regular APIRest service is managing via flask. I am having trouble setting parameters for my script commands. In particular:

a) How to set a default value within the @manager.options line

b) How to set a flag

I have tried something like the following:

@manager.option('-m', '--credit', dest='credit', default=-1, help='regular value')
@manager.option('--useCredit', dest='useCredit', default=False, help='boolean')
def newClient(credit=100, useCredit=False):

I managed to "hack" a default value by adding this to the function variable, but then I do not know what the "default" parameter is for in @manager.option? In addition, useCredit always asks me for a value, how can I make it a flag?

update

Thanks to @carlos answer I realized that I must had been testing it wrong, as it DOES work to set default parameters within the manager.option command. It seems that any values I place in the function definition (def newClient(credit=100, useCredit=False) are ignored. I would had expected some warning or error by the system...

XAnguera
  • 1,157
  • 1
  • 11
  • 25

1 Answers1

2

I'm not terribly sure where the problem lies but doing a quick mock up of your code didn't seem to exhibit the same error as yours did:

from flask.ext.script import Manager
from flask import Flask

app = Flask(__name__)

manager = Manager(app)

@manager.option('-m', '--credit', dest='credit', default=-1)
@manager.option('-u', '--use_credit', dest='use_credit', default=False)
def new_client(credit, use_credit):
    if credit is None:
        print "credit is", credit
    else:
        print "credit is", credit, "with use_credit", use_credit

if __name__ == "__main__":
    manager.run()

The resulting output:

enter image description here

Carlos
  • 1,897
  • 3
  • 19
  • 37
  • Thanks @carlos, indeed I must had been testing it wrong. Still, in your tests you have not addressed my question about the boolean values. It looks like I can set up a boolean as a default parameter, but how do I pass a boolean input from command line without using flags? and how to use flags here? – XAnguera Mar 16 '15 at 10:24
  • So, you're trying to pass a boolean through the command line with the explicit use of a flag? Like so: `python sample.py new_client --credit=100 True` ? – Carlos Mar 16 '15 at 18:26
  • I do not understand your statement/question. I would like to pass a flag (i.e. parameter without explicit value) but it seems that it is impossible... or at least the documentation is not clear about it and I did not find out how. – XAnguera Mar 17 '15 at 15:09
  • Ah, now I understand what you mean. You're looking to add "optional arguments" just like here: https://docs.python.org/2/howto/argparse.html#introducing-optional-arguments. That being said, you're in luck as Flask-Script uses argparse behind the scenes. If this is truly important to you then take a look at the Miguel Grinberg example he did with "Flask-Runner". https://github.com/miguelgrinberg/Flask-Runner/blob/master/flask_runner.py. In it, take a look at `def get_options(self)`, lines 21 to 24, for example. That, along with the Python doc, will get you what you want. – Carlos Mar 17 '15 at 23:32