tomerfiliba / plumbum

Plumbum: Shell Combinators
https://plumbum.readthedocs.io
MIT License
2.8k stars 182 forks source link

Is it possible to easily print the values of cli.SwitchAttr? #549

Closed wtanksleyjr closed 3 years ago

wtanksleyjr commented 3 years ago

As someone just trying to use the new-to-me feature of cli.SwitchAttr and Flag, I'd like to quickly print the value loaded from the command line (or environment variable).

I wrote is:

CACHE_FILE = cli.SwitchAttr("--cache-file", WriteableFile, envname="AUDIOBOOKS_CACHE_FILE")
print(f"cache: {CACHE_FILE}", file=sys.stderr)

What I see is: cache: <plumbum.cli.switches.SwitchAttr object at 0x7fa1e5f54ac0> What I expected to see is: cache: ./testfile.txt

(WriteableFile is my own attempt to make a validator, of course it's part of why I'm inserting print statements, but this also happens with cli.Flag and no validation.)

So... how to I get the actual value that was passed (and/or returned by the validator, I'm not sure what's expected to happen there), and why is doing this so surprisingly non-obvious, when the documentation makes it look like everything's all the same as normal Python?

wtanksleyjr commented 3 years ago

I figured this out, I think -- I'm not sure, but it looks like I'm supposed to access the SwitchAttr variable as self.CACHE_FILE (to use the above example), except sometimes when that's not needed. I'm not sure what's going on there, but I was able to solve my specific problem. I think.