From 78eecfc81a0d957cf6480c9438252bb113f0e97d Mon Sep 17 00:00:00 2001 From: Janis Hutz Date: Thu, 14 May 2026 17:36:06 +0200 Subject: [PATCH] feat(cli): Better organized CLI arg parsing --- archmgr.py | 7 ++++++- cli/args.py | 44 ++++++++---------------------------------- cli/commands/commit.py | 13 +++++++++++++ cli/commands/config.py | 21 ++++++++++++++++++++ cli/commands/init.py | 11 +++++++++++ cli/commands/pull.py | 12 ++++++++++++ cli/commands/push.py | 11 +++++++++++ cli/commands/show.py | 12 ++++++++++++ commands/config.py | 12 +++++++----- commands/show.py | 8 +++++--- config/__init__.py | 1 - templates/README.md | 1 + templates/__init__.py | 2 ++ 13 files changed, 109 insertions(+), 46 deletions(-) create mode 100644 cli/commands/commit.py create mode 100644 cli/commands/config.py create mode 100644 cli/commands/init.py create mode 100644 cli/commands/pull.py create mode 100644 cli/commands/push.py create mode 100644 cli/commands/show.py diff --git a/archmgr.py b/archmgr.py index 88795aa..20dd98e 100755 --- a/archmgr.py +++ b/archmgr.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # PYTHON_ARGCOMPLETE_OK +from typing import cast import cli.args as cliargs import commands.commit as commit @@ -9,7 +10,9 @@ import commands.init as init import commands.pull as pull import commands.push as push import commands.prepare as setup +import commands.show as show from config import load_config +from templates import ArchMgrTemplates if __name__ == "__main__": args, ap = cliargs.add_cli_args() @@ -35,7 +38,7 @@ if __name__ == "__main__": if args.cmd == "commit": commit.commit(conf, args.force, args.no_render) elif args.cmd == "config": - config.config(conf) + config.config(args, conf) elif args.cmd == "init": init.init(args.force) elif args.cmd == "setup": @@ -44,6 +47,8 @@ if __name__ == "__main__": pull.pull(conf, args.rebase, args.apply) elif args.cmd == "push": push.push(args.force) + elif args.cmd == "show": + show.show(conf, cast(ArchMgrTemplates, {}), args) except KeyboardInterrupt as e: exit(130) except Exception as e: diff --git a/cli/args.py b/cli/args.py index bba75b6..47fd392 100644 --- a/cli/args.py +++ b/cli/args.py @@ -1,6 +1,8 @@ import argparse import argcomplete +from cli.commands import commit, config, init, pull, push, show + def add_cli_args(): ap = argparse.ArgumentParser( @@ -18,43 +20,13 @@ def add_cli_args(): # ┌ ┐ # │ Subcommands │ # └ ┘ - commit = sp.add_parser( - "commit", help="apply pending changes and commit to git repo" - ) - commit.add_argument("-f", "--force", help="force apply (skips prompts)", action="store_true") - commit.add_argument( - "--no-render", "-r", help="do not re-render renderables", action="store_true" - ) - - # TODO: Allow changing things like config path - sp.add_parser("config", help="prints information about your config and change some of them") - sp.add_parser("setup", help="Do initial setup, like installing required tools") - - init = sp.add_parser("init", help="initialize a new archmgr repository") - init.add_argument( - "--force", - "-f", - help="resets the git repository and initializes new archmgr repo", - action="store_true", - ) - - push = sp.add_parser("push", help="push pending changes to git remote") - push.add_argument( - "--force", - "-f", - help="force push (overriding possible remote changes)", - action="store_true", - ) - - pull = sp.add_parser("pull", help="pull changes from git remote") - pull.add_argument("--rebase", "-r", help="execute rebase", action="store_true") - pull.add_argument( - "--apply", - "-a", - help="also apply the changes to the local system", - action="store_true", - ) + commit.add_parser(sp) + config.add_parser(sp) + init.add_parser(sp) + pull.add_parser(sp) + push.add_parser(sp) + show.add_parser(sp) argcomplete.autocomplete(ap) diff --git a/cli/commands/commit.py b/cli/commands/commit.py new file mode 100644 index 0000000..b093823 --- /dev/null +++ b/cli/commands/commit.py @@ -0,0 +1,13 @@ +import argparse as ap + + +def add_parser(sp: ap._SubParsersAction[ap.ArgumentParser]): + commit = sp.add_parser( + "commit", help="apply pending changes and commit to git repo" + ) + commit.add_argument( + "-f", "--force", help="force apply (skips prompts)", action="store_true" + ) + commit.add_argument( + "--no-render", "-r", help="do not re-render renderables", action="store_true" + ) diff --git a/cli/commands/config.py b/cli/commands/config.py new file mode 100644 index 0000000..d95ac70 --- /dev/null +++ b/cli/commands/config.py @@ -0,0 +1,21 @@ +import argparse as ap + + +def add_parser(sp: ap._SubParsersAction[ap.ArgumentParser]): + config = sp.add_parser( + "config", help="prints information about your config and change some of them" + ) + conf_sp = config.add_subparsers(title="config options", dest="conf", required=True) + conf_sp.add_parser("show", help="show config options") + config_update = conf_sp.add_parser( + "update", help="update the configuration from various" + ) + conf_update_sp = config_update.add_subparsers( + title="Options for automatic configuration updates", + dest="conf_update", + required=True, + ) + conf_update_sp.add_parser( + "pkgs", + help="Update the config to reflect the package state of the current system", + ) diff --git a/cli/commands/init.py b/cli/commands/init.py new file mode 100644 index 0000000..f8a22a2 --- /dev/null +++ b/cli/commands/init.py @@ -0,0 +1,11 @@ +import argparse as ap + + +def add_parser(sp: ap._SubParsersAction[ap.ArgumentParser]): + init = sp.add_parser("init", help="initialize a new archmgr repository") + init.add_argument( + "--force", + "-f", + help="resets the git repository and initializes new archmgr repo", + action="store_true", + ) diff --git a/cli/commands/pull.py b/cli/commands/pull.py new file mode 100644 index 0000000..533ecfb --- /dev/null +++ b/cli/commands/pull.py @@ -0,0 +1,12 @@ +import argparse as ap + + +def add_parser(sp: ap._SubParsersAction[ap.ArgumentParser]): + pull = sp.add_parser("pull", help="pull changes from git remote") + pull.add_argument("--rebase", "-r", help="execute rebase", action="store_true") + pull.add_argument( + "--apply", + "-a", + help="also apply the changes to the local system", + action="store_true", + ) diff --git a/cli/commands/push.py b/cli/commands/push.py new file mode 100644 index 0000000..e3d4cc2 --- /dev/null +++ b/cli/commands/push.py @@ -0,0 +1,11 @@ +import argparse as ap + + +def add_parser(sp: ap._SubParsersAction[ap.ArgumentParser]): + push = sp.add_parser("push", help="push pending changes to git remote") + push.add_argument( + "--force", + "-f", + help="force push (overriding possible remote changes)", + action="store_true", + ) diff --git a/cli/commands/show.py b/cli/commands/show.py new file mode 100644 index 0000000..5e3ac6c --- /dev/null +++ b/cli/commands/show.py @@ -0,0 +1,12 @@ +import argparse as ap + + +def add_parser(sp: ap._SubParsersAction[ap.ArgumentParser]): + show = sp.add_parser("show", help="get information about configuration presets") + show_sp = show.add_subparsers( + title="config presets to show details on", + dest="show", + required=True, + ) + pkgs = show_sp.add_parser("pkgs", help="show details on package presets") + pkgs.add_argument("show_pkg") diff --git a/commands/config.py b/commands/config.py index 4bc74b6..f1642c7 100644 --- a/commands/config.py +++ b/commands/config.py @@ -1,8 +1,10 @@ +import argparse from config.dtype import ArchMgrConfig -def config(config: ArchMgrConfig): - print(""" - Your config can be found at - """) - print(config) +def config(args: argparse.Namespace, config: ArchMgrConfig): + if args.conf == "show": + print(""" + Your config can be found at + """) + print(config) diff --git a/commands/show.py b/commands/show.py index 0953172..270d90e 100644 --- a/commands/show.py +++ b/commands/show.py @@ -1,4 +1,4 @@ -from typing import Literal +import argparse from config.dtype import ArchMgrConfig from templates import ArchMgrTemplates @@ -6,7 +6,9 @@ from templates import ArchMgrTemplates # TODO: Templates data def show( - config: ArchMgrConfig, spec: ArchMgrTemplates, kind: Literal["bundle"], arg: str + config: ArchMgrConfig, + spec: ArchMgrTemplates, + args: argparse.Namespace, ): """Show details / information about things like contents pkg bundles @@ -16,4 +18,4 @@ def show( kind: The type of information to get arg: The arg for the command (e.g. bundle name) """ - pass + print(args) diff --git a/config/__init__.py b/config/__init__.py index e25ed07..6de8f26 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -85,7 +85,6 @@ def load_config(file: str) -> ArchMgrConfig: conf = cast(ArchMgrConfig, configuration) # Recursively load files - print("Requires", requires) for conf_file in requires: conf = merge_configs(conf, load_config(conf_file)) diff --git a/templates/README.md b/templates/README.md index 5427ec9..4cbd40a 100644 --- a/templates/README.md +++ b/templates/README.md @@ -5,3 +5,4 @@ To function, it needs both a configuration file (or multiple) and templates for The latter reside in this folder and are a stripped-down version of the default config file. ## Template config file +TODO: Restructure this folder diff --git a/templates/__init__.py b/templates/__init__.py index c40204d..96913a2 100644 --- a/templates/__init__.py +++ b/templates/__init__.py @@ -10,6 +10,8 @@ class ArchMgrTemplates: class BootLoaderSettings: templates: ArchMgrTemplateData + theme_folder: str + conf_build_cmd: str name: str