Skip to content

[feat] CLI-02/C-6: argparse-style args module#11

Merged
vt128 merged 1 commit into
masterfrom
feat/cli-02-args
Jun 21, 2026
Merged

[feat] CLI-02/C-6: argparse-style args module#11
vt128 merged 1 commit into
masterfrom
feat/cli-02-args

Conversation

@vt128

@vt128 vt128 commented Jun 21, 2026

Copy link
Copy Markdown
Member

CLI-02 / C-6 β€” args, a Python argparse-style argument parser

Per the maintainer's call to make argument parsing "more like Python and easy to
understand", this adds an args module whose surface deliberately mirrors
argparse:

load("args", "ArgumentParser")

p = ArgumentParser(description = "greet someone")
p.add_argument("--name", default = "World", help = "who to greet")
p.add_argument("--count", type = int, default = 1)
p.add_argument("--shout", action = "store_true")
p.add_argument("file", help = "input file")

ns = p.parse_args()                 # parses argv[1:]
print(ns.name, ns.count, ns.shout, ns.file)
$ ./starcli greet.star -- --name Kevin --count 3 --shout in.txt
Kevin 3 True in.txt

Surface

  • ArgumentParser(prog, description) β†’ add_argument(name, type, default, help, required, action="store_true") β†’ parse_args(argv=None) β†’ a Namespace
    struct (ns.name). Plus format_help() and args.argv.
  • type accepts the Python builtins (int/float/str/bool) or the
    equivalent string.
  • argv[0] is the script name (or -c), like Python sys.argv; parse_args
    defaults to argv[1:]. Handles --name val, --name=val, store_true
    flags, positionals, the -- terminator, and required checks.
  • Errors read prog: error: <msg> and fail the run with a non-zero exit.

Wiring

Added to the default module set (mods.go) and classified CapPure in the
capability gate (pure parsing of the captured argv β€” loads in every tier).

Table-driven tests cover parsing / types / errors / the argv[1:] default / the
-- terminator / help. README documents it. Coverage 76.8% (β‰₯ 65); Docker
golang:1.22 green.

Part of CLI-02 (STAR-19). Remaining there: stdin/pipe streaming (C-3).

Add an 'args' module that parses a script's own command-line arguments with a
surface that deliberately mirrors Python's argparse, so it reads familiarly:

  load("args", "ArgumentParser")
  p = ArgumentParser(description = "greet")
  p.add_argument("--name", default = "World", help = "who")
  p.add_argument("--count", type = int, default = 1)
  p.add_argument("--shout", action = "store_true")
  p.add_argument("file")
  ns = p.parse_args()              # parses argv[1:]
  print(ns.name, ns.count, ns.shout, ns.file)

- ArgumentParser(prog, description) -> add_argument(name, type, default, help,
  required, action='store_true') -> parse_args(argv=None) -> Namespace struct.
  Plus format_help(). type accepts the Python builtins (int/float/str/bool) or
  the equivalent string.
- argv[0] is the script name (or '-c'), like Python sys.argv; parse_args
  defaults to argv[1:]. Supports '--name val', '--name=val', store_true flags,
  positionals, the '--' terminator, and required checks; errors read
  'prog: error: <msg>' and fail the run with a non-zero exit.
- Wired into mods.go (default module set) and classified CapPure in the
  capability gate (pure parsing of the captured argv).

Table-driven tests cover parsing, types, errors, the argv[1:] default, the
terminator, and help. README documents it. Coverage 76.8%; Docker golang:1.22
green.
@vt128 vt128 merged commit baf8b73 into master Jun 21, 2026
6 checks passed
@vt128 vt128 deleted the feat/cli-02-args branch June 21, 2026 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant