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

use default value for required arg if not provided #763

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

pkbhowmick
Copy link
Collaborator

what

why

references

Signed-off-by: Pulak Kanti Bhowmick <pkbhowmick007@gmail.com>
coderabbitai[bot]
coderabbitai bot previously approved these changes Nov 5, 2024
@osterman osterman added the minor New features that do not break anything label Nov 6, 2024
Co-authored-by: Erik Osterman (CEO @ Cloud Posse) <erik@cloudposse.com>
coderabbitai[bot]
coderabbitai bot previously approved these changes Nov 7, 2024
@osterman
Copy link
Member

osterman commented Nov 9, 2024

@pkbhowmick this PR is now in conflict

@osterman
Copy link
Member

Please add something to this page: https://atmos.tools/core-concepts/custom-commands/

@osterman
Copy link
Member

@coderabbitai can you suggest the changes to documentation related to this PR

Copy link
Contributor

coderabbitai bot commented Nov 11, 2024

Tip

For best results, initiate chat on the files or code changes.

@osterman, here's a suggestion for updating the documentation related to this PR:

Add a section to the Custom Commands documentation explaining that atmos now uses the default value for a required argument if it's not provided. This enhances flexibility when invoking custom commands.

For example, you could add:

### Default Values for Required Arguments

Atmos allows you to define default values for required arguments in your custom commands. If a required argument is not provided when the command is invoked, atmos will use the specified default value.

**Example:**

Given a custom command configuration:

```yaml
commands:
  deploy-stack:
    description: "Deploy a stack."
    arguments:
      - name: "environment"
        description: "The environment to deploy."
        required: true
        default: "dev"

You can now run:

atmos deploy-stack

Atmos will use "dev" as the default value for the environment argument.


This update will inform users about the new behavior introduced in this PR.

@pkbhowmick
Copy link
Collaborator Author

pkbhowmick commented Dec 4, 2024

Hi @osterman
In my findings, I have found cobra cli only support for default value of flags only, we can't pass default value of args.

I believe there is order related issue too for commands who have multiple arguments. Please correct me in case I miss something.

@osterman
Copy link
Member

osterman commented Dec 4, 2024

I think you're right that it doesn't support it natively, but I think they address the use-case in their docs.

https://github.com/spf13/cobra/blob/main/site/content/user_guide.md#positional-and-custom-arguments

The gist of it is I think you need to check args directly, and use the defaults manually.

Example of the logic:

var exampleCmd = &cobra.Command{
    Use:   "example [arg1] [arg2]",
    Short: "An example command",
    Args:  cobra.MaximumNArgs(2),
    Run: func(cmd *cobra.Command, args []string) {
        arg1 := "default1"
        arg2 := "default2"

        if len(args) > 0 {
            arg1 = args[0]
        }
        if len(args) > 1 {
            arg2 = args[1]
        }

        fmt.Printf("Arg1: %s, Arg2: %s\n", arg1, arg2)
    },
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
minor New features that do not break anything
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

2 participants