Skip to content

Commit 18d1e5a

Browse files
committed
refactor: be explicit about the args with the Parse funcs
1 parent 9418245 commit 18d1e5a

File tree

4 files changed

+59
-54
lines changed

4 files changed

+59
-54
lines changed

builder_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func TestBuilder(t *testing.T) {
115115
NewCmd("root").
116116
Opt(NewOpt("aa").Short('a')).
117117
Opt(NewOpt("bb").Short('b')).
118-
ParseOrExit([]string{}...)
118+
ParseTheseOrExit()
119119
},
120120
func() {
121121
NewCmd("root").

cli.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,12 @@ func GetAllSeq[T any](c *Command, id string) iter.Seq[T] {
243243
// will print the help message and exit the program successfully (status code 0). If
244244
// there is any other error, it will print the error and exit the program with failure
245245
// (status code 1). The input parameter semantics are the same as [CommandInfo.Parse].
246-
func (in CommandInfo) ParseOrExit(args ...string) *Command {
247-
c, err := in.Parse(args...)
246+
func (in CommandInfo) ParseOrExit() *Command {
247+
return in.ParseTheseOrExit(os.Args[1:]...)
248+
}
249+
250+
func (in CommandInfo) ParseTheseOrExit(args ...string) *Command {
251+
c, err := in.ParseThese(args...)
248252
if err != nil {
249253
if e, ok := err.(HelpRequestError); ok {
250254
fmt.Print(e.HelpMsg)
@@ -259,14 +263,15 @@ func (in CommandInfo) ParseOrExit(args ...string) *Command {
259263

260264
// ParseOrExit will parse input based on this CommandInfo. If no function arguments
261265
// are provided, the [os.Args] will be used.
262-
func (in *CommandInfo) Parse(args ...string) (*Command, error) {
266+
func (in *CommandInfo) Parse() (*Command, error) {
267+
return in.ParseThese(os.Args[1:]...)
268+
}
269+
270+
func (in *CommandInfo) ParseThese(args ...string) (*Command, error) {
263271
if !in.isPrepped {
264272
in.prepareAndValidate()
265273
in.isPrepped = true
266274
}
267-
if args == nil {
268-
args = os.Args[1:]
269-
}
270275
c := &Command{
271276
Inputs: make([]Input, 0, len(args)),
272277
}

cli_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ func TestParsing(t *testing.T) {
531531
t.Setenv(k, v)
532532
}
533533

534-
got, gotErr := tt.cmd.Parse(tio.args...)
534+
got, gotErr := tt.cmd.ParseThese(tio.args...)
535535
if tio.expErr != nil && gotErr == nil {
536536
t.Fatalf("expected error %[1]T: %[1]v, got no error", tio.expErr)
537537
}
@@ -656,7 +656,7 @@ func TestOptLookups(t *testing.T) {
656656

657657
// with both options present
658658
{
659-
c := in.ParseOrExit("-ahello", "-bworld")
659+
c := in.ParseTheseOrExit("-ahello", "-bworld")
660660
// straight getting the opts
661661
{
662662
optA := Get[string](c, "a")
@@ -683,7 +683,7 @@ func TestOptLookups(t *testing.T) {
683683

684684
// with only the first option 'a' present
685685
{
686-
c := in.ParseOrExit("-ahello")
686+
c := in.ParseTheseOrExit("-ahello")
687687
// first one should be there, second one shouldn't
688688
{
689689
optA, ok := Lookup[string](c, "a")
@@ -740,7 +740,7 @@ func TestOptLookups(t *testing.T) {
740740

741741
// with both options present
742742
{
743-
c := in.ParseOrExit("-ahello", "-bworld")
743+
c := in.ParseTheseOrExit("-ahello", "-bworld")
744744
// straight getting the opts
745745
{
746746
optA := Get[string](c, "a")
@@ -767,7 +767,7 @@ func TestOptLookups(t *testing.T) {
767767

768768
// with only the first option 'a' provided
769769
{
770-
c := in.ParseOrExit("-ahello")
770+
c := in.ParseTheseOrExit("-ahello")
771771
// first one should be there, second one default
772772
{
773773
optA, ok := Lookup[string](c, "a")
@@ -812,7 +812,7 @@ func TestArgLookups(t *testing.T) {
812812

813813
// with both args present
814814
{
815-
c := in.ParseOrExit("hello", "world")
815+
c := in.ParseTheseOrExit("hello", "world")
816816
// straight getting the args
817817
{
818818
arg1 := Get[string](c, "arg1")
@@ -839,7 +839,7 @@ func TestArgLookups(t *testing.T) {
839839

840840
// with only the first arg 'arg1' present
841841
{
842-
c := in.ParseOrExit("hello")
842+
c := in.ParseTheseOrExit("hello")
843843
// first one should be there, second one shouldn't
844844
{
845845
arg1, ok := Lookup[string](c, "arg1")
@@ -933,7 +933,7 @@ options:
933933
`,
934934
},
935935
} {
936-
_, err := tt.cmd.Parse(tt.cliArgs...)
936+
_, err := tt.cmd.ParseThese(tt.cliArgs...)
937937
gotHelpMsg := err.Error()
938938
if gotHelpMsg != tt.expHelpMsg {
939939
t.Errorf("%s: expected:\n%s\ngot:\n%s", tt.Case, tt.expHelpMsg, gotHelpMsg)

0 commit comments

Comments
 (0)