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

1

7

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?

Prostatectomy answered 21/1, 2020 at 13:41 Comment(5)
Seems this is answered already: #15009258Doy
Would making a flag called no-my-param be okay for you?Appetizing
@Doy Your reference is for argparse, not for Luigi. Are you suggesting they have the exact same mechanism?Prostatectomy
@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...Prostatectomy
Ah I see. Here is the answer: github.com/spotify/luigi/issues/193#issuecomment-25854409 or you override the cmdline_parser: luigi.readthedocs.io/en/stable/_modules/luigi/…Doy
P
6

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
Prostatectomy answered 21/1, 2020 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.