3

I have a Luigi task with a boolean parameter that is set to True by default:

class MyLuigiTask(luigi.Task):
    my_bool_param = luigi.BoolParameter(default=True) 

When I run this task from terminal, I sometimes want to pass that parameter as False, but get the following result:

$ MyLuigiTask --my_bool_param False
error: unrecognized arguments: False  

Same obviously for false and 0...

I understand that I can make the default False and then use the flag --my_bool_param if I want to make it True, but I much prefer to have the default True.

Is there any way to do this, and still pass False from terminal?

DalyaG
  • 2,067
  • 2
  • 12
  • 14
  • 1
    Seems this is answered already: https://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse – Tom Jan 21 '20 at 13:54
  • Would making a flag called `no-my-param` be okay for you? – Simon Crane Jan 21 '20 at 13:58
  • @Tom Your reference is for argparse, not for Luigi. Are you suggesting they have the exact same mechanism? – DalyaG Jan 21 '20 at 14:04
  • @SimonCrane thanks for the suggestion, it is "ok" the same as making the default False is ok. I am wondering though if there is something I am missing with passing "False" to Luigi... – DalyaG Jan 21 '20 at 14:05
  • Ah I see. Here is the answer: https://github.com/spotify/luigi/issues/193#issuecomment-25854409 or you override the cmdline_parser: https://luigi.readthedocs.io/en/stable/_modules/luigi/cmdline_parser.html – Tom Jan 21 '20 at 15:44

1 Answers1

3

Found the solution in Luigi docs:

class MyLuigiTask(luigi.Task):
    my_bool_param = luigi.BoolParameter(
        default=True, 
        parsing=luigi.BoolParameter.EXPLICIT_PARSING)

    def run(self):
        print(self.my_bool_param)

Here EXPLICIT_PARSING tell Luigi that adding the flag --my_bool_param false in the terminal call to MyLuigiTask, will be parsed as store_false.

Now we can have:

$ MyLuigiTask --my_bool_param false
False
DalyaG
  • 2,067
  • 2
  • 12
  • 14