Python Forum
key=value style command argument parser
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
key=value style command argument parser
#6
Skaperen Wrote:i did not see anything about key=value arguments in that module, either.

Here is an example with plumbum.cli, a modified version of an example from the documentation
import logging
from plumbum import cli

logger = logging.getLogger(__name__)

class MyApp(cli.Application):
    @cli.switch("--log-to-file", str)
    def log_to_file(self, filename):
        print('Logging to file', filename)
        logger.addHandler(logging.FileHandler(filename))

    @cli.switch("--verbose", requires = ["--log-to-file"], excludes = ["--terse"])
    def verbose(self):
        logger.setLevel(logging.DEBUG)

    @cli.switch("--terse", requires = ["--log-to-file"], excludes = ["--verbose"])
    def terse(self):
        logger.setLevel(logging.WARNING)

    def main(self):
        logger.info('Execution of' + __file__)

if __name__ == "__main__":
    MyApp.run()
Output:
λ python3 paillasse/truccli.py --log-to-file=spam.txt --verbose Logging to file spam.txt
As you can see, I used --log-to-file=spam.txt and it worked.
Reply


Messages In This Thread
RE: key=value style command argument parser - by Gribouillis - Dec-02-2019, 08:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  my own command line option parser Skaperen 0 1,674 Mar-27-2020, 04:14 AM
Last Post: Skaperen
  argument for the python command Skaperen 0 2,657 Dec-05-2019, 11:30 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020