os.Args tripping up on certain words

polaris · · 574 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>In my program, I&#39;m parsing the arguments passed in on the command line to perform certain actions. However, it appears that if os.Args[1:] contains the word &#34;do&#34; and perhaps others, my whole program does nothing. I&#39;ve tested this by removing &#34;do&#34; and it seems to be okay. Is there a way around this? I know that &#34;do&#34; is a reserved word in my command line, but shouldn&#39;t arguments following a program invocation not be treated as such? Not exactly sure why it&#39;s happening. Thanks in advance for any advice!</p> <hr/>**评论:**<br/><br/>jamesrr39: <pre><p>do you quote do when you run your program, so it looks like <code>./myApp do</code> or <code>./myApp &#39;do&#39;</code>? And which shell are you using - bash?</p></pre>ConfuciusBateman: <pre><p>I&#39;m using zsh, and actually I just narrowed it down to what I think is the culprit: when the args contain &#34;=&gt;&#34;. And no, nothing is quoted when running ./myApp fun args go here =&gt; stuff stuff.</p> <p>I get this error when =&gt; is at the end of the arguments: zsh: parse error near `\n&#39;</p> <p>If =&gt; is anywhere in the middle, such as ./myProgram word word =&gt; more words, it simply exits without a message. Any ideas? =&gt; doesn&#39;t seem to be an alias for anything, so it&#39;s sort of weird. Not sure if it&#39;s worth copying the function that is causing this, it&#39;s extremely simple and just printing out the full os.Args slice so I can see what&#39;s going on. </p> <p>Quick edit: even just echoing &#34;=&gt;&#34; in my shell causes the same error, so I&#39;m quite sure this is independent of my program.</p></pre>justinisrael: <pre><p>Zsh also interprets other characters as special, such as a question mark (?) when the arg might be a url with a query string. You have to enclose those args in quotes to prevent the shell from interpreting them</p></pre>ConfuciusBateman: <pre><p>Got it, in fact I just realized it&#39;s not the fat arrow, it&#39;s specifically &#39;&gt;&#39;. </p> <p>Would there be any other possible solution? As what I&#39;m trying to do involves running my program with the arrow to signify something special about the text that follows. Perhaps I just need to choose a different symbol, because I don&#39;t want to have to type </p> <pre><code>./myProgram add thing &#34;=&gt;&#34; here&#39;s the special text </code></pre></pre>justinisrael: <pre><p>Yea, you just have to accept the fact that you are using a special stream redirection symbol in a shell command. Either quote it, or quote the entire text string as a whole, or pick another symbol.</p></pre>justinisrael: <pre><p>I don&#39;t know why it would be os.Args tripping up on certain words. It just contains the args passed when the process was executed. It is probably your shell command. What kind of command are you running? They may never have made it to the actual Go process. </p></pre>skelterjohn: <pre><p>If you post the source to a simple program (say, one that prints the args) and a script that runs it to reproduce, I&#39;m sure we can figure out what the issue is.</p> <p>It&#39;s almost certainly an issue with how you&#39;re invoking the program, rather than an issue with the program itself.</p></pre>drvd: <pre><p>This has absolutely nothing to do with os.Args as os.Args just transports the arguments from program invocation to your code and does not do anything beside that, especially not look at the arguments provided. Stop looking where nothing is hiding and focus on what you do wrong invoking your program, especially when invoking through a shell which might do tons of stuff to what you type (and see in the terminal) before handing it down to your code.</p></pre>ConfuciusBateman: <pre><p>I responded to another person in the thread, but I believe I found the source of the problem and I don&#39;t think it has anything to do with my program. It appears that when a fat arrow (&#34;=&gt;&#34;) is present in the arguments to the program, zshell doesn&#39;t like it. Even if I just run echo =&gt;, I get:</p> <pre><code>zsh: parse error near `\n&#39; </code></pre> <p>Another quick edit: it&#39;s actually just &#39;&gt;&#39;, not the arrow.</p></pre>

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

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