tomerfiliba / plumbum

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

[question] How to force plumbum to process selected CLI switch first? #633

Open vient opened 1 year ago

vient commented 1 year ago

Hi, it may be easier to show in code what I mean:

import logging
from plumbum import cli

logging.basicConfig(level=logging.INFO)

class A(cli.Application):
    @cli.autoswitch()
    def debug(self):
        logging.basicConfig(level=logging.DEBUG, force=True)
        logging.debug('enabled debug logs')

    @cli.autoswitch()
    def another_option(self):
        logging.debug('enabled another option')

    def main(self, *args):
        logging.info('in main')

A.run()

when run produces

$ ./lul.py
INFO:root:in main

$ ./lul.py --debug --another-option
DEBUG:root:enabled debug logs
DEBUG:root:enabled another option
INFO:root:in main

$ ./lul.py --another-option --debug
DEBUG:root:enabled debug logs
INFO:root:in main

Options are clearly processed in the order they occur in command line but in this case I would prefer to scan command line first for --debug option since if affects logs from other switches. I tried to extract --debug option in separate class DebugOptionProcessor(cli.Application) class and then use class A(DebugOptionProcessor) but it did not change anything. Is it possible to do?