MCPcopy Create free account
hub / github.com/cli/cli / parseRunArgs

Function parseRunArgs

script/ghmock/run.go:82–134  ·  view source on GitHub ↗
(args []string)

Source from the content-addressed store, hash-verified

80}
81
82func parseRunArgs(args []string) (runOptions, error) {
83 opts := runOptions{}
84 fs := flag.NewFlagSet("run", flag.ContinueOnError)
85 fs.SetOutput(io.Discard)
86 fs.Var((*stringList)(&opts.binaries), "gh", "gh binary to run; repeat to compare binaries")
87 fs.BoolVar(&opts.tty, "tty", false, "make gh behave as if writing to a 120-column terminal")
88 fs.BoolVar(&opts.terminal, "terminal", false, "run gh in a real terminal, so it can prompt")
89 fs.Var((*stringList)(&opts.keys), "keys", "keys to type at the next prompt, such as my-repo{enter}; repeat for each prompt")
90 fs.StringVar(&opts.stdin, "stdin", "", "text to pass to gh on standard input")
91 fs.BoolVar(&opts.loggedOut, "logged-out", false, "run gh without a token")
92 fs.BoolVar(&opts.localhost, "localhost", false, "use github.localhost over plain HTTP, for gh builds that ignore SSL_CERT_FILE")
93 fs.StringVar(&opts.configDir, "config-dir", "", "copy synthetic gh configuration from this fixture directory")
94 fs.StringVar(&opts.in, "in", "", "directory inside the prepared Git workspace")
95 fs.DurationVar(&opts.timeout, "timeout", defaultTimeout, "how long each gh run may take")
96 if err := fs.Parse(args); err != nil {
97 return runOptions{}, usageError{err}
98 }
99 opts.args = fs.Args()
100 configSpecified := false
101 fs.Visit(func(f *flag.Flag) {
102 if f.Name == "config-dir" {
103 configSpecified = true
104 }
105 })
106 if len(opts.keys) > 0 {
107 opts.terminal = true
108 }
109
110 switch {
111 case len(opts.binaries) == 0:
112 return runOptions{}, newUsageError("at least one --gh is required")
113 case len(opts.args) == 0:
114 return runOptions{}, newUsageError("missing gh arguments; put them after --")
115 case opts.timeout <= 0:
116 return runOptions{}, newUsageError("--timeout must be positive")
117 case configSpecified && opts.configDir == "":
118 return runOptions{}, newUsageError("--config-dir needs a fixture directory")
119 case opts.configDir != "" && opts.loggedOut:
120 return runOptions{}, newUsageError("--config-dir and --logged-out cannot be combined; use a fixture without hosts.yml for a logged-out run")
121 case opts.in != "" && !filepath.IsLocal(opts.in):
122 return runOptions{}, newUsageError("--in must be a relative path inside the Git workspace")
123 case opts.terminal && opts.tty:
124 return runOptions{}, newUsageError("--terminal and --keys already give gh a terminal; leave out --tty")
125 case opts.terminal && opts.stdin != "":
126 return runOptions{}, newUsageError("in a terminal, gh reads what you type; use --keys instead of --stdin")
127 }
128 for _, k := range opts.keys {
129 if _, err := parseKeys(k); err != nil {
130 return runOptions{}, usageError{err}
131 }
132 }
133 return opts, nil
134}
135
136func cmdRun(args []string, stdout, stderr io.Writer) error {
137 opts, err := parseRunArgs(args)

Callers 2

cmdRunFunction · 0.85
TestParseRunArgsFunction · 0.85

Calls 3

newUsageErrorFunction · 0.85
parseKeysFunction · 0.85
IsLocalMethod · 0.65

Tested by 1

TestParseRunArgsFunction · 0.68