flag2 - an alternate command line flag library for Go

blov · · 439 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p><a href="https://github.com/ProfOak/flag2" rel="nofollow">https://github.com/ProfOak/flag2</a></p> <p>Hey all, I made a flag library for Go. I was not satisfied with the flag library that comes with the standard library. My issue was that the flags did not follow anything I was used to. You could use full words with the single dash: <code>-flag</code>. This also prevents you from stringing multiple single character arguments in a single dash: <code>-asdf</code> meaning <code>-a -s -d -f</code>.</p> <p>I realize my library is a little rough around the edges but I&#39;m pretty satisfied with how it&#39;s turning out so far.</p> <p>Readme: <a href="https://github.com/ProfOak/flag2/blob/master/README.md" rel="nofollow">https://github.com/ProfOak/flag2/blob/master/README.md</a></p> <p>Exhaustive example: <a href="https://github.com/ProfOak/flag2/tree/master/test" rel="nofollow">https://github.com/ProfOak/flag2/tree/master/test</a></p> <hr/>**评论:**<br/><br/>raff99: <pre><p>Given the name, I would have liked something with an interface to the &#34;flag&#34; package.</p> <p>For example you could use the same interface and make the first parameter (flag name) support something like &#34;short|long&#34; (where short would be the one-letter flag name and long the expanded name).</p> <p>That way one could just import &#34;flag2&#34; as flag and get the new behaviour.</p></pre>ProfOak_: <pre><p>That&#39;s a fair point. I hadn&#39;t considered the name implying that before, but I get what you&#39;re saying. I may change the name in the near future. I just wanted to get my code out there for more eyes to judge it.</p></pre>jtblin: <pre><p>How does that compare to <a href="https://github.com/spf13/pflag" rel="nofollow">https://github.com/spf13/pflag</a>?</p></pre>ProfOak_: <pre><p>Great question! I had not seen that before I started this project. </p> <p>It looks like we share the same goal with traditional posix style flags. But the first thing that jumps out at me is implementation. The setup code for pflag looks more like the flag library that comes with go.</p> <pre><code>var ip *int = flag.Int(&#34;flagname&#34;, 1234, &#34;help message for flagname&#34;) var flagvar int flag.IntVar(&amp;flagvar, &#34;flagname&#34;, 1234, &#34;help message for flagname&#34;) </code></pre> <p>My library behaves more like Python&#39;s optparse library. One of the reasons I came to like Go is because I had a lot of fun programming in it, similar to the way I felt about programming in Python.</p> <p>The other thing is that flag2 generates a help message for the programmer to call within their code. I didn&#39;t read anything like that on the pflag readme.</p> <p><a href="https://github.com/ProfOak/flag2/blob/master/test/test.go#L66" rel="nofollow">f.Usage()</a></p> <p><a href="https://github.com/ProfOak/flag2/blob/master/test/README.md" rel="nofollow">How the generated text looks</a></p> <p>optparse in python:</p> <pre><code>from optparse import OptionParser parser = OptionParser() parser.add_option(&#34;-f&#34;, &#34;--file&#34;, dest=&#34;filename&#34;, help=&#34;write report to FILE&#34;, metavar=&#34;FILE&#34;) parser.add_option(&#34;-q&#34;, &#34;--quiet&#34;, action=&#34;store_false&#34;, dest=&#34;verbose&#34;, default=True, help=&#34;don&#39;t print status messages to stdout&#34;) (options, args) = parser.parse_args() </code></pre> <p>flag2 in Go (modified version of the code on my readme):</p> <pre><code>package main import ( &#34;os&#34; &#34;fmt&#34; &#34;github.com/ProfOak/flag2&#34; ) func main() { f := flag2.NewFlag() f.AddString(&#34;n&#34;, &#34;name&#34;, &#34;this flag wants a name as input&#34;, &#34;billy&#34;) f.AddBool(&#34;b&#34;, &#34;bool&#34;, &#34;this flag will store true if used&#34;, false) options, args := f.Parse(os.Args) } </code></pre></pre>gavv42: <pre><p>BTW, if it&#39;s like <code>optparse</code> and you&#39;re thinking about renaming the package, you could rename it to <code>optparse</code>.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

439 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传