Search code examples
pythonterminalluigi

Luigi: Is there a way to pass 'false' to a bool parameter from the command line?


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?


Solution

  • 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