Skip to content

Commit

Permalink
feat: support running commands via non-command line arguments
Browse files Browse the repository at this point in the history
Signed-off-by: oninowang <oninowang@tencent.com>
  • Loading branch information
oninowang committed Jan 30, 2024
1 parent 4fb7396 commit b8bc01d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
21 changes: 14 additions & 7 deletions os/gcmd/gcmd_command_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ import (
"github.com/gogf/gf/v2/util/gutil"
)

// Run calls custom function that bound to this command.
// Run calls custom function in os.Args that bound to this command.
// It exits this process with exit code 1 if any error occurs.
func (c *Command) Run(ctx context.Context) {
_ = c.RunWithValue(ctx)
}

// RunWithValue calls custom function that bound to this command with value output.
// RunWithValue calls custom function in os.Args that bound to this command with value output.
// It exits this process with exit code 1 if any error occurs.
func (c *Command) RunWithValue(ctx context.Context) (value interface{}) {
value, err := c.RunWithValueError(ctx)
Expand Down Expand Up @@ -64,20 +64,27 @@ func (c *Command) RunWithValue(ctx context.Context) (value interface{}) {
return value
}

// RunWithError calls custom function that bound to this command with error output.
// RunWithError calls custom function in os.Args that bound to this command with error output.
func (c *Command) RunWithError(ctx context.Context) (err error) {
_, err = c.RunWithValueError(ctx)
return
}

// RunWithValueError calls custom function that bound to this command with value and error output.
// RunWithValueError calls custom function in os.Args that bound to this command with value and error output.
func (c *Command) RunWithValueError(ctx context.Context) (value interface{}, err error) {
// Parse command arguments and options using default algorithm.
parser, err := Parse(nil)
return c.RunWithSpecificArgs(ctx, os.Args)
}

// RunWithSpecificArgs calls custom function in specific args that bound to this command with value and error output.
func (c *Command) RunWithSpecificArgs(ctx context.Context, args []string) (value interface{}, err error) {
if len(args) == 0 {
return nil, gerror.NewCode(gcode.CodeInvalidParameter, "args can not be empty!")
}
parser, err := ParseArgs(args, nil)
if err != nil {
return nil, err
}
args := parser.GetArgAll()
args = parser.GetArgAll()
if len(args) == 1 {
return c.doRun(ctx, parser)
}
Expand Down
5 changes: 5 additions & 0 deletions os/gcmd/gcmd_z_unit_feature_object1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ func Test_Command_NewFromObject_RunWithValue(t *testing.T) {
value3, err3 := cmd.RunWithValueError(ctx)
t.AssertNil(err3)
t.Assert(value3, `{"Name":"tom","Version":true}`)

// test empty args
value4, err4 := cmd.RunWithSpecificArgs(ctx, nil)
t.Assert(err4, "args can not be empty!")
t.Assert(value4, nil)
})
}

Expand Down

0 comments on commit b8bc01d

Please sign in to comment.