Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add help message when no arguments are provided #455

Merged
merged 1 commit into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cmd/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const (

var (
packagesPresenterOpt packages.PresenterOption
packagesArgs = cobra.MinimumNArgs(1)
packagesArgs = cobra.MaximumNArgs(1)
packagesCmd = &cobra.Command{
Use: "packages [SOURCE]",
Short: "Generate a package SBOM",
Expand All @@ -65,8 +65,7 @@ var (
if err != nil {
return err
}
// silently exit
return fmt.Errorf("")
return fmt.Errorf("an image/directory argument is required")
}

// set the presenter
Expand Down
10 changes: 9 additions & 1 deletion cmd/power_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,19 @@ var powerUserCmd = &cobra.Command{
"appName": internal.ApplicationName,
"command": "power-user",
}),
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
Hidden: true,
SilenceUsage: true,
SilenceErrors: true,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
err := cmd.Help()
if err != nil {
return err
}
return fmt.Errorf("an image/directory argument is required")
}

if appConfig.Dev.ProfileCPU && appConfig.Dev.ProfileMem {
return fmt.Errorf("cannot profile CPU and memory simultaneously")
}
Expand Down
9 changes: 9 additions & 0 deletions test/cli/packages_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ func TestPackagesCmdFlags(t *testing.T) {
env map[string]string
assertions []traitAssertion
}{
{
name: "no-args-shows-help",
args: []string{"packages"},
assertions: []traitAssertion{
assertInOutput("an image/directory argument is required"), // specific error that should be shown
assertInOutput("Generate a packaged-based Software Bill Of Materials"), // excerpt from help description
assertFailingReturnCode,
},
},
{
name: "json-output-flag",
args: []string{"packages", "-o", "json", request},
Expand Down
9 changes: 9 additions & 0 deletions test/cli/power_user_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ func TestPowerUserCmdFlags(t *testing.T) {
env map[string]string
assertions []traitAssertion
}{
{
name: "no-args-shows-help",
args: []string{"power-user"},
assertions: []traitAssertion{
assertInOutput("an image/directory argument is required"), // specific error that should be shown
assertInOutput("Run bulk operations on container images"), // excerpt from help description
assertFailingReturnCode,
},
},
{
name: "json-output-flag-fails",
args: []string{"power-user", "-o", "json", "docker-archive:" + getFixtureImage(t, "image-pkg-coverage")},
Expand Down