/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: Teddy Hogeborn
  • Date: 2019-03-03 22:09:38 UTC
  • Revision ID: teddy@recompile.se-20190303220938-92e8q07rtjdnpjh6
mandos-ctl: Refactor

* mandos-ctl (commands_and_clients_from_options): Take options as an
                                                  argument.  Move
                                                  parser setup into
                                                  new function and
                                                  move extra parse
                                                  checking into main.
  (add_command_line_options): New.
  (main): Create parser here and call add_command_line_options() to
          add options to it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
516
516
                options.approve,
517
517
                options.deny))
518
518
 
519
 
 
520
 
def commands_and_clients_from_options(args=None):
521
 
    if args is None:
522
 
        args=sys.argv[1:]
523
 
    parser = argparse.ArgumentParser()
 
519
def add_command_line_options(parser):
524
520
    parser.add_argument("--version", action="version",
525
521
                        version="%(prog)s {}".format(version),
526
522
                        help="show version number and exit")
583
579
    parser.add_argument("--check", action="store_true",
584
580
                        help="Run self-test")
585
581
    parser.add_argument("client", nargs="*", help="Client name")
586
 
    options = parser.parse_args(args=args)
587
 
 
588
 
    if has_actions(options) and not (options.client or options.all):
589
 
        parser.error("Options require clients names or --all.")
590
 
    if options.verbose and has_actions(options):
591
 
        parser.error("--verbose can only be used alone.")
592
 
    if options.dump_json and (options.verbose
593
 
                              or has_actions(options)):
594
 
        parser.error("--dump-json can only be used alone.")
595
 
    if options.all and not has_actions(options):
596
 
        parser.error("--all requires an action.")
597
 
    if options.is_enabled and len(options.client) > 1:
598
 
            parser.error("--is-enabled requires exactly one client")
 
582
 
 
583
 
 
584
def commands_and_clients_from_options(options):
599
585
 
600
586
    commands = []
601
587
 
670
656
 
671
657
 
672
658
def main():
673
 
    commands, clientnames = commands_and_clients_from_options()
 
659
    parser = argparse.ArgumentParser()
 
660
 
 
661
    add_command_line_options(parser)
 
662
 
 
663
    options = parser.parse_args()
 
664
 
 
665
    if has_actions(options) and not (options.client or options.all):
 
666
        parser.error("Options require clients names or --all.")
 
667
    if options.verbose and has_actions(options):
 
668
        parser.error("--verbose can only be used alone.")
 
669
    if options.dump_json and (options.verbose
 
670
                              or has_actions(options)):
 
671
        parser.error("--dump-json can only be used alone.")
 
672
    if options.all and not has_actions(options):
 
673
        parser.error("--all requires an action.")
 
674
    if options.is_enabled and len(options.client) > 1:
 
675
        parser.error("--is-enabled requires exactly one client")
 
676
 
 
677
    commands, clientnames = commands_and_clients_from_options(options)
674
678
 
675
679
    try:
676
680
        bus = dbus.SystemBus()