|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8" /> |
| 5 | +<meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 6 | +<title>flagtype - pressly/cli</title> |
| 7 | +<meta name="description" content="Common flag.Value implementations for pressly/cli: StringSlice, Enum, EnumDefault, StringMap, URL, Regexp." /> |
| 8 | +<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Ctext y='14' font-size='14'%3E%E2%96%B6%3C/text%3E%3C/svg%3E" /> |
| 9 | +<link rel="stylesheet" href="styles.css" /> |
| 10 | +<script> |
| 11 | + (function () { |
| 12 | + try { |
| 13 | + var t = localStorage.getItem("theme"); |
| 14 | + if (t === "dark" || t === "light") { |
| 15 | + document.documentElement.setAttribute("data-theme", t); |
| 16 | + } |
| 17 | + } catch (_) {} |
| 18 | + })(); |
| 19 | +</script> |
| 20 | +</head> |
| 21 | +<body> |
| 22 | + |
| 23 | +<header class="site-header"> |
| 24 | + <div class="wrap"> |
| 25 | + <a class="brand" href="./">pressly/<strong>cli</strong></a> |
| 26 | + <nav class="nav"> |
| 27 | + <a href="./#quickstart">Quick start</a> |
| 28 | + <a href="./#api">API</a> |
| 29 | + <a href="./#examples">Examples</a> |
| 30 | + <div class="nav-menu"> |
| 31 | + <a class="nav-menu-trigger" href="./#subpackages" aria-haspopup="true">Subpackages <span class="caret" aria-hidden="true">▼</span></a> |
| 32 | + <div class="nav-menu-items" role="menu"> |
| 33 | + <a href="flagtype.html" role="menuitem">flagtype</a> |
| 34 | + <a href="graceful.html" role="menuitem">graceful</a> |
| 35 | + <a href="xflag.html" role="menuitem">xflag</a> |
| 36 | + </div> |
| 37 | + </div> |
| 38 | + <a href="./#why">But why?</a> |
| 39 | + <span class="nav-sep" aria-hidden="true"></span> |
| 40 | + <a href="https://pkg.go.dev/github.com/pressly/cli/flagtype" target="_blank" rel="noopener">godoc</a> |
| 41 | + <a href="https://github.com/pressly/cli/tree/main/flagtype" target="_blank" rel="noopener">github</a> |
| 42 | + <button id="theme-toggle" type="button" aria-label="toggle dark mode" title="toggle dark mode"> |
| 43 | + <svg class="i-sun" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="4"/><path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M4.93 19.07l1.41-1.41M17.66 6.34l1.41-1.41"/></svg> |
| 44 | + <svg class="i-moon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/></svg> |
| 45 | + </button> |
| 46 | + </nav> |
| 47 | + </div> |
| 48 | +</header> |
| 49 | + |
| 50 | +<main class="wrap content" id="top"> |
| 51 | + |
| 52 | + <section class="intro"> |
| 53 | + <h1>flagtype</h1> |
| 54 | + <p class="lede"> |
| 55 | + Common <code>flag.Value</code> implementations for <code>pressly/cli</code>. Each constructor |
| 56 | + returns a value you register with <code>f.Var()</code>; storage is internal, and values are |
| 57 | + retrieved through <code>cli.GetFlag[T]</code>. |
| 58 | + </p> |
| 59 | +<pre><code class="language-go">import "github.com/pressly/cli/flagtype"</code></pre> |
| 60 | + </section> |
| 61 | + |
| 62 | + <hr /> |
| 63 | + |
| 64 | + <section> |
| 65 | + <h2 id="index">At a glance</h2> |
| 66 | + <p>Each symbol links to its <a href="https://pkg.go.dev/github.com/pressly/cli/flagtype" target="_blank" rel="noopener">godoc</a> entry, the canonical reference.</p> |
| 67 | +<pre class="api-index"><code><span class="idx-h">FUNCTIONS</span> |
| 68 | + <a href="https://pkg.go.dev/github.com/pressly/cli/flagtype#StringSlice" target="_blank" rel="noopener">func StringSlice() flag.Value</a> |
| 69 | + <a href="https://pkg.go.dev/github.com/pressly/cli/flagtype#Enum" target="_blank" rel="noopener">func Enum(allowed ...string) flag.Value</a> |
| 70 | + <a href="https://pkg.go.dev/github.com/pressly/cli/flagtype#EnumDefault" target="_blank" rel="noopener">func EnumDefault(defaultVal string, allowed []string) flag.Value</a> |
| 71 | + <a href="https://pkg.go.dev/github.com/pressly/cli/flagtype#StringMap" target="_blank" rel="noopener">func StringMap() flag.Value</a> |
| 72 | + <a href="https://pkg.go.dev/github.com/pressly/cli/flagtype#URL" target="_blank" rel="noopener">func URL() flag.Value</a> |
| 73 | + <a href="https://pkg.go.dev/github.com/pressly/cli/flagtype#Regexp" target="_blank" rel="noopener">func Regexp() flag.Value</a></code></pre> |
| 74 | + </section> |
| 75 | + |
| 76 | + <hr /> |
| 77 | + |
| 78 | + <section> |
| 79 | + <h2 id="patterns">Patterns</h2> |
| 80 | + <p>Register a value with <code>f.Var()</code>, retrieve it with the matching <code>cli.GetFlag[T]</code>. The full set, in one place:</p> |
| 81 | +<pre><code class="language-go">cmd := &cli.Command{ |
| 82 | + Name: "fetch", |
| 83 | + Flags: cli.FlagsFunc(func(f *flag.FlagSet) { |
| 84 | + f.Var(flagtype.StringSlice(), |
| 85 | + "tag", "add a tag (repeatable)") |
| 86 | + |
| 87 | + f.Var(flagtype.Enum("json", "yaml", "table"), |
| 88 | + "format", "output format") |
| 89 | + |
| 90 | + f.Var(flagtype.EnumDefault("sql", []string{"sql", "go"}), |
| 91 | + "type", "migration type") |
| 92 | + |
| 93 | + f.Var(flagtype.StringMap(), |
| 94 | + "label", "key=value (repeatable)") |
| 95 | + |
| 96 | + f.Var(flagtype.URL(), |
| 97 | + "endpoint", "API endpoint") |
| 98 | + |
| 99 | + f.Var(flagtype.Regexp(), |
| 100 | + "match", "filter pattern") |
| 101 | + }), |
| 102 | + Exec: func(ctx context.Context, state *cli.State) error { |
| 103 | + tags := cli.GetFlag[[]string](state, "tag") |
| 104 | + format := cli.GetFlag[string](state, "format") |
| 105 | + migType := cli.GetFlag[string](state, "type") |
| 106 | + labels := cli.GetFlag[map[string]string](state, "label") |
| 107 | + endpoint := cli.GetFlag[*url.URL](state, "endpoint") |
| 108 | + re := cli.GetFlag[*regexp.Regexp](state, "match") |
| 109 | + |
| 110 | + _, _, _, _, _, _ = tags, format, migType, labels, endpoint, re |
| 111 | + return nil |
| 112 | + }, |
| 113 | +}</code></pre> |
| 114 | +<pre><code>$ fetch --help |
| 115 | +Usage: |
| 116 | + fetch [flags] |
| 117 | + |
| 118 | +Flags: |
| 119 | + --endpoint url API endpoint |
| 120 | + --format enum output format |
| 121 | + --label stringMap key=value (repeatable) |
| 122 | + --match regexp filter pattern |
| 123 | + --tag stringSlice add a tag (repeatable) |
| 124 | + --type enum migration type (default: sql)</code></pre> |
| 125 | + <p>Each flagtype carries a type hint that shows up in the auto-generated help. Repeatable flags (<code>StringSlice</code>, <code>StringMap</code>) collect values across multiple appearances on the command line:</p> |
| 126 | +<pre><code class="language-bash">fetch --tag a --tag b --label env=prod --label tier=api</code></pre> |
| 127 | + </section> |
| 128 | + |
| 129 | + <footer class="site-footer"> |
| 130 | + <p> |
| 131 | + <a href="./">back to cli</a> |
| 132 | + · |
| 133 | + <a href="https://pkg.go.dev/github.com/pressly/cli/flagtype" target="_blank" rel="noopener">pkg.go.dev/flagtype</a> |
| 134 | + </p> |
| 135 | + </footer> |
| 136 | + |
| 137 | +</main> |
| 138 | + |
| 139 | +<script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/components/prism-core.min.js"></script> |
| 140 | +<script src="https://cdn.jsdelivr.net/npm/prismjs@1.29.0/plugins/autoloader/prism-autoloader.min.js"></script> |
| 141 | +<script src="app.js"></script> |
| 142 | + |
| 143 | +</body> |
| 144 | +</html> |
0 commit comments