Skip to content

How to parse a string array #1429

Answered by meatballhat
guptanitin280 asked this question in Q&A
Discussion options

You must be logged in to vote

@guptanitin280 If your intent is for ["hello", "world"] to be parsed as a []string{}, then cli.Context.Args is probably not what you want.

The primary intended use of cli.Context.Args is to allow arbitrary positional arguments to be passed through (un-parsed!) to a given action.

For example:

package main

import (
        "encoding/json"
        "fmt"
        "log"
        "os"

        "github.com/urfave/cli/v2"
)

func main() {
        sentenceFlag := &cli.StringSliceFlag{Name: "sentence"}
        wordFlag := &cli.StringFlag{Name: "word"}

        app := &cli.App{
                Action: func(cCtx *cli.Context) error {
                        jb, err := json.MarshalIndent(map[string]int…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by meatballhat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1427 on June 25, 2022 13:32.