EXE之后的命令行参数

huangapple go评论75阅读模式
英文:

Command Line Arguments after EXE

问题

我有我的脚本"file.go"通过"go build file.go"构建,现在我有了"file.exe"。

在代码中,我有"steamid := xxxxxxxxxxxxxxxx"。有没有办法在命令提示符中执行"file.exe"时使用"file.exe -steamid=xxxxxxxxxxxxxxxx"?

代码:

package main

import (
	"crypto/md5"
	"fmt"
)

func main() {
	steamid := xxxxxxxxxxxxxxxx

	h := md5.New()
	h.Write([]byte("BE"))

	for i := 0; i < 8; i++ {
		h.Write([]byte{byte(steamid & 0xFF)})
		steamid >>= 8
	}

	fmt.Printf("Battleye GUID: %x", h.Sum(nil))
}

我已经做到了这一步,有新的回复:

package main

import (
	"crypto/md5"
	"fmt"
	"bufio"
	"os"
	"flag"
)

var SteamID string

func init() {
	flag.StringVar(&SteamID, "steamid", "XXXXXXXXXXXXXXXXX", "17位数字的SteamID")
}

func main() {
	steamid := &SteamID

	h := md5.New()
	h.Write([]byte("BE"))

	for i := 0; i < 8; i++ {
		h.Write([]byte{byte(*steamid & 0xFF)})
		*steamid >>= 8
	}

	fmt.Printf("Battleye GUID: %x", h.Sum(nil))
	fmt.Print("\n按下'Enter'继续...")
	bufio.NewReader(os.Stdin).ReadBytes('\n')
}

错误

C:\Go\bin>go build file.go
# command-line-arguments
.\file.go:24: invalid operation: steamid & 255 (类型不匹配 *string  int)
.\file.go:25: invalid operation: steamid >>= 8 (类型 *string 不能进行位移操作)

<details>
<summary>英文:</summary>

I have my script &quot;file.go&quot; Built with &quot;go build file.go&quot; now I have &quot;file.exe&quot;

In the code I have &quot;steamid := xxxxxxxxxxxxxxxx&quot; Is there anyway when executing file.exe in cmd like &quot;file.exe -steamid=xxxxxxxxxxxxxxxx&quot;

code:

    package main
     
    import (
    	&quot;crypto/md5&quot;
    	&quot;fmt&quot;
    )
     
    func main() {
      steamid := xxxxxxxxxxxxxxxx
     
    	h := md5.New()
    	h.Write([]byte(&quot;BE&quot;))
     
    	for i := 0; i &lt; 8; i++ {
    		h.Write([]byte{byte(steamid &amp; 0xFF)})
    		steamid &gt;&gt;= 8
    	}
     
    	fmt.Printf(&quot;Battleye GUID: %x&quot;, h.Sum(nil))
    }

----

I&#39;ve gotten as far as here with new replys;

    package main
     
    import (
    	&quot;crypto/md5&quot;
    	&quot;fmt&quot;
    	&quot;bufio&quot;
    	&quot;os&quot;
    	&quot;flag&quot;
    )
    
    var SteamID string
    
    func init() {
        flag.StringVar(&amp;SteamID, &quot;steamid&quot;, &quot;XXXXXXXXXXXXXXXXX&quot;, &quot;17 Numbers SteamID&quot;)
    }
     
    func main() {
    	steamid := &amp;SteamID
     
    	h := md5.New()
    	h.Write([]byte(&quot;BE&quot;))
     
    	for i := 0; i &lt; 8; i++ {
    		h.Write([]byte{byte(steamid &amp; 0xFF)})
    		steamid &gt;&gt;= 8
    	}
     
    	fmt.Printf(&quot;Battleye GUID: %x&quot;, h.Sum(nil))
    	fmt.Print(&quot;\nPress &#39;Enter&#39; to continue...&quot;)
    	bufio.NewReader(os.Stdin).ReadBytes(&#39;\n&#39;) 
    }

Error: 

    C:\Go\bin&gt;go build file.go
    # command-line-arguments
    .\file.go:24: invalid operation: steamid &amp; 255 (mismatched types *string and int)
    .\file.go:25: invalid operation: steamid &gt;&gt;= 8 (shift of type *string)

</details>


# 答案1
**得分**: 2

标准库中包含的[flag包][1]可以实现这一功能

在你的脚本中需要添加以下内容

```go
var SteamID string

func init() {
    flag.StringVar(&SteamID, "steamid", "<插入默认值>", "<插入帮助文本>")
}

(如果你需要将其作为整数获取,可以使用[Int64Var][2])
然后在你的主函数中添加:

flag.Parse()

这将初始化`SteamID`的值

[1]: http://golang.org/pkg/flag/
[2]: http://golang.org/pkg/flag/#Int64Var
英文:

the flag package included in the standard library does just that.

what you need to add in your script:

<!-- language: go -->

var SteamID string
func init() {
flag.StringVar(&amp;SteamID, &quot;steamid&quot;, &quot;&lt;insert default value&gt;&quot;, &quot;&lt;insert help text&gt;&quot;)
}

(in case you need to get it as an integer, use Int64Var instead)
then in your main function add:

flag.Parse()

This will initialise the value of SteamID

答案2

得分: 0

错误信息中已经说明了问题所在。你不能对字符串、指向字符串的指针或任何非整数类型进行位操作,你需要先将它们转换或解析为整数。使用strconv包中的strconv.ParseInt及其相关函数来解析字符串。

parsedID, e := strconv.ParseInt(*steamID, 16, 64)
if e != nil { log.Fatal("无法解析ID") }
// 使用parsedID。
英文:

It's all in the error message. You can't do bitwise operations with strings, pointers to strings or anything that is not an integer, you need to convert or parse them into integers first. Use strconv.ParseInt and its friends from the strconv package to parse strings.

parsedID, e := strconv.ParseInt(*steamID, 16, 64)
if e != nil { log.Fatal(&quot;couldn&#39;t parse the ID&quot;) }
// Use parsedID.

huangapple
  • 本文由 发表于 2014年6月24日 21:09:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/24387528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定