Skip to content

Commit c8775e7

Browse files
committed
Move main CLI execution into a __main__ check
1 parent 5e854ba commit c8775e7

File tree

1 file changed

+56
-51
lines changed

1 file changed

+56
-51
lines changed

bin/dots

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ warnings.showwarning = handle_warning
171171
def git_diff(git_args, path1, path2):
172172
subprocess.call(['git', 'diff', '--diff-filter=MA'] + git_args + [path1, path2])
173173

174+
174175
class Configuration(object):
175176

176177
# This is a list of groups that are only a single directory deep and do not
@@ -539,70 +540,74 @@ class ConfigFile(object):
539540
# Run install scripts
540541
if exec_install_scripts: self.exec_install_scripts(to)
541542

542-
# Setup the argument parser
543-
parser = argparse.ArgumentParser(add_help=False)
544543

545-
# Handle getting the configuration path
546-
parser.add_argument('-c', '--config', default=Configuration.default_group_file)
544+
def setup_argparser():
545+
parser = argparse.ArgumentParser(add_help=False)
546+
547+
# Handle getting the configuration path
548+
parser.add_argument('-c', '--config', default=Configuration.default_group_file)
549+
550+
# Setup the sub-commands
551+
sub_commands = parser.add_subparsers(dest='command')
552+
553+
# The main sub commands
554+
groups = sub_commands.add_parser('groups', add_help=False)
555+
diff = sub_commands.add_parser('diff', add_help=False)
556+
files = sub_commands.add_parser('files', add_help=False)
557+
install = sub_commands.add_parser('install', add_help=False)
558+
helps = sub_commands.add_parser('help', add_help=False)
547559

548-
# Setup the sub-commands
549-
sub_commands = parser.add_subparsers(dest='command')
560+
# Groups sub commands
561+
groups_commands = groups.add_subparsers(dest='group_command')
550562

551-
# The main sub commands
552-
groups = sub_commands.add_parser('groups', add_help=False)
553-
diff = sub_commands.add_parser('diff', add_help=False)
554-
files = sub_commands.add_parser('files', add_help=False)
555-
install = sub_commands.add_parser('install', add_help=False)
556-
helps = sub_commands.add_parser('help', add_help=False)
563+
groups_known = groups_commands.add_parser('known', add_help=False)
564+
groups_current = groups_commands.add_parser('current', add_help=False)
565+
groups_clear = groups_commands.add_parser('clear', add_help=False)
566+
groups_set = groups_commands.add_parser('set', add_help=False)
557567

558-
# Groups sub commands
559-
groups_commands = groups.add_subparsers(dest='group_command')
568+
# Groups-set options
569+
groups_set.add_argument('group', nargs='+')
560570

561-
groups_known = groups_commands.add_parser('known', add_help=False)
562-
groups_current = groups_commands.add_parser('current', add_help=False)
563-
groups_clear = groups_commands.add_parser('clear', add_help=False)
564-
groups_set = groups_commands.add_parser('set', add_help=False)
571+
# Diff options
572+
diff.add_argument('file', nargs='*')
565573

566-
# Groups-set options
567-
groups_set.add_argument('group', nargs='+')
574+
# Files options
575+
files.add_argument('file', nargs='*')
568576

569-
# Diff options
570-
diff.add_argument('file', nargs='*')
577+
# Install options
578+
install.add_argument('-l', '--location', default=INSTALL_DIR)
579+
install.add_argument('file', nargs='*')
571580

572-
# Files options
573-
files.add_argument('file', nargs='*')
581+
return parser
574582

575-
# Install options
576-
install.add_argument('-l', '--location', default=INSTALL_DIR)
577-
install.add_argument('file', nargs='*')
578583

579-
# Parse arguments!
580-
args, extra_args = parser.parse_known_args()
584+
if __name__ == '__main__':
585+
args, extra_args = setup_argparser().parse_known_args()
581586

582-
config = Configuration(group_file=args.config)
583-
config.load_from_file()
587+
config = Configuration(group_file=args.config)
588+
config.load_from_file()
584589

585-
if args.command == 'groups':
586-
if args.group_command == 'known':
587-
print('\n'.join(config.valid_groups + ['']), end='')
588-
elif args.group_command == 'current':
589-
print('\n'.join(config.groups + ['']), end='')
590-
elif args.group_command == 'set':
591-
config.groups = args.group
592-
config.save_to_file()
593-
elif args.group_command == 'clear':
594-
config.groups = []
595-
config.save_to_file()
590+
if args.command == 'groups':
591+
if args.group_command == 'known':
592+
print('\n'.join(config.valid_groups + ['']), end='')
593+
elif args.group_command == 'current':
594+
print('\n'.join(config.groups + ['']), end='')
595+
elif args.group_command == 'set':
596+
config.groups = args.group
597+
config.save_to_file()
598+
elif args.group_command == 'clear':
599+
config.groups = []
600+
config.save_to_file()
596601

597-
elif args.command == 'diff':
598-
config.diff_installed(extra_args, args.file)
602+
elif args.command == 'diff':
603+
config.diff_installed(extra_args, args.file)
599604

600-
elif args.command == 'files':
601-
files = [f.path for f in config.files(args.file)]
602-
for file in sorted(files): print(file)
605+
elif args.command == 'files':
606+
files = [f.path for f in config.files(args.file)]
607+
for file in sorted(files): print(file)
603608

604-
elif args.command == 'install':
605-
config.install_tree(args.location, args.file, True)
609+
elif args.command == 'install':
610+
config.install_tree(args.location, args.file, True)
606611

607-
elif args.command == 'help':
608-
print(USAGE)
612+
elif args.command == 'help':
613+
print(USAGE)

0 commit comments

Comments
 (0)