feat(cli): Better organized CLI arg parsing

This commit is contained in:
2026-05-14 17:36:06 +02:00
parent e24eb647ca
commit 78eecfc81a
13 changed files with 109 additions and 46 deletions
+6 -1
View File
@@ -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:
+8 -36
View File
@@ -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)
+13
View File
@@ -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"
)
+21
View File
@@ -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",
)
+11
View File
@@ -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",
)
+12
View File
@@ -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",
)
+11
View File
@@ -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",
)
+12
View File
@@ -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")
+7 -5
View File
@@ -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)
+5 -3
View File
@@ -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)
-1
View File
@@ -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))
+1
View File
@@ -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
+2
View File
@@ -10,6 +10,8 @@ class ArchMgrTemplates:
class BootLoaderSettings:
templates: ArchMgrTemplateData
theme_folder: str
conf_build_cmd: str
name: str