Go CLI Patterns

blov · · 684 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey guys, </p> <p>looking for some patterns / examples of Go CLI tools. what are the best ways to implement it </p> <p>thanks!</p> <hr/>**评论:**<br/><br/>lktslionel: <pre><p>Cobra (<a href="https://github.com/spf13/cobra">https://github.com/spf13/cobra</a>) is a great choice. Even big projects (kubernetes &amp; more) use it. It propose a cli app generator built in that help you bootstrap a cli project in Go.</p></pre>etherealflaim: <pre><p>Just don&#39;t follow the examples that encourage the rampant use of globals...</p></pre>heartwilltell: <pre><p>I think that Cobra is great but it is a little bit complex for beginners. </p></pre>dan_82: <pre><p>Wow. cool. I wonder if there is anything similar to Python Plumbum that allows to call commands and remote commands (and makes it easier to make wrappers).</p></pre>superchalupa: <pre><p>This link was posted a few weeks ago: <a href="https://blog.depado.eu/post/checklist-for-go-projects" rel="nofollow">https://blog.depado.eu/post/checklist-for-go-projects</a></p> <p>It has concrete and simple examples for Viper and Cobra integration. I integrated these both with my project and am very happy with the results.</p></pre>darklumt: <pre><p>I have used <a href="https://github.com/urfave/cli">cli</a> with great success, its pretty simple to use and with great features.</p></pre>FatFingerHelperBot: <pre><p>It seems that your comment contains 1 or more links that are hard to tap for mobile users. I will extend those so they&#39;re easier for our sausage fingers to click!</p> <p><a href="https://github.com/urfave/cli" rel="nofollow">Here is link number 1</a> - Previous text &#34;cli&#34;</p> <hr/> <p><sup>Please</sup> <sup>PM</sup> <sup><a href="/u/eganwall" rel="nofollow">/u/eganwall</a></sup> <sup>with</sup> <sup>issues</sup> <sup>or</sup> <sup>feedback!</sup> <sup>|</sup> <sup><a href="https://reddit.com/message/compose/?to=FatFingerHelperBot&amp;subject=delete&amp;message=delete%20dxk5ki4" rel="nofollow">Delete</a></sup></p></pre>xopherus: <pre><p>My team uses urfave as well. It&#39;s been very effective for us.</p></pre>Redundancy_: <pre><p>I do wish that there was a little more there for dealing with required arguments, but I&#39;ve used it in a number of projects without fuss.</p> <p>It&#39;s nice that it now supports a function that can return an error, although I do find wrapping the error in the cli.NewError a little annoying (understandable for having a return code, but I don&#39;t think the default behaviour worked for me).</p></pre>__crackers__: <pre><p><a href="https://github.com/alecthomas/kingpin">kingpin</a> is the most capable library, imo.</p> <p>Cobra does weird stuff (e.g. camelCase flags like <code>--logFile</code> instead of <code>--log-file</code>). Theoretically, its integration with viper should be a killer feature, but that&#39;s been <a href="https://github.com/spf13/viper/issues/276">(somewhat) broken for 18 months</a>.</p> <p><a href="https://github.com/docopt/docopt.go">docopt</a> is good for simpler programs.</p></pre>jh125486: <pre><p>I also like how easy it is to define command line arts that can also be ENV cars. That was weird to do in cobra/ufave. </p> <p>Oh and Count of args... like with “-vvv” I can set the log level +3 times for super duper verbose, or just “-v” for regular verbose. </p></pre>kalekold: <pre><p>I&#39;ve written a few and most times you don&#39;t need to use a framework.</p> <p>Here&#39;s one I wrote a while back</p> <p><a href="https://github.com/nomad-software/grope" rel="nofollow">https://github.com/nomad-software/grope</a></p></pre>saturn_vk: <pre><p>I simply user flagsets for the different subcommands. So far I haven&#39;t had a case that wasn&#39;t easily solvable</p></pre>daveddev: <pre><p>If you&#39;d like to avoid third-party dependencies, this is a simple way to add subcommands (it looks more complicated than it is): <a href="https://play.golang.org/p/kcuxbVlBrd-" rel="nofollow">https://play.golang.org/p/kcuxbVlBrd-</a></p> <p>And running a subcommand: <a href="https://play.golang.org/p/BYrN5N9LSCt" rel="nofollow">https://play.golang.org/p/BYrN5N9LSCt</a></p> <p>As with postponing/avoiding the use of http frameworks, new users would probably benefit from learning this approach before reaching for a cli framework.</p></pre>earthboundkid: <pre><p>I actually like the flag package (weird, I know). I usually try to write a very short main function that just calls something to turn flags into local variables, and then calls that thing and if it returns an error exits with a non-zero error code. </p> <p>Here is a decent example: <a href="https://github.com/baltimore-sun-data/boreas/blob/master/main.go" rel="nofollow">https://github.com/baltimore-sun-data/boreas/blob/master/main.go</a></p></pre>blackflicker: <pre><p>If it&#39;s a simple CLI tool just use <code>os.Args</code> and raw-string literals, do not be fancy at the beginning. Don&#39;t even start with <a href="https://golang.org/pkg/flag/" rel="nofollow">flag package</a>. If it&#39;s more complex than try flag pkg first and check out <a href="https://github.com/avelino/awesome-go#command-line" rel="nofollow">here</a>.</p></pre>

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

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