获取一个目录名称作为参数。

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

Go obtain a directory name as an argument

问题

获取一个以 Cyrillic 字符作为参数的目录名称

如何获取完整路径,而不是截断的垃圾信息。
示例:作为程序参数的路径 /home/spouk/spouk.download/torrent/Сергей Лукьяненко, Собрание сочинений/

我得到的是修剪过的版本,如何修复?
/Home/spouk/spouk.download/torrent/Сергей

简单代码示例

package main

import (
    "flag"
    "fmt"
)

func main() {
    wordPtr := flag.String("path", "foo", "a string")
    flag.Parse()
    fmt.Printf("Flag: -path=`%v`\nflag.Args: %v\n", *wordPtr, flag.Args())
    fmt.Printf("Rune: %v\n", []rune(*wordPtr))
}
英文:

Go obtain a directory name from the сyrillic as an argument

How to get the full path, not cut garbage.
Example: The path as a program argument /home/spouk/spouk.download/torrent/Сергей Лукьяненко, Собрание сочинений/

I get in the trimmed version of the program, how to fix?
/Home/spouk/spouk.download/torrent/Сергей

example simple code

package main

import (
	"flag"
	"fmt"
)

func main() {
	wordPtr := flag.String("path", "foo", "a string")
	flag.Parse()
	fmt.Printf("Flag: -path=`%v`\nflag.Args: %v\n", *wordPtr, flag.Args())
	fmt.Printf("Rune: %v\n", []rune(*wordPtr))
}

答案1

得分: 4

这不是Go代码的问题,而是如何将其传递到程序中的问题,请尝试使用双引号 "…" 将路径传递进去。

./main -path="/home/spouk/spouk.download/torrent/Сергей Лукьяненко, Собрание сочинений/"
> Flag: -path=`/home/spouk/spouk.download/torrent/Сергей Лукьяненко, Собрание сочинений/`
> flag.Args: []
> Rune: [47 104 111 109 101 47 115 112 111 117 107 47 115 112 111 117 107 46 100 111 119 110 108 111 97 100 47 116 111 114 114 101 110 116 47 1057 1077 1088 1075 1077 1081 32 1051 1091 1082 1100 1103 1085 1077 1085 1082 1086 44 32 1057 1086 1073 1088 1072 1085 1080 1077 32 1089 1086 1095 1080 1085 1077 1085 1080 1081 47]
英文:

This is not a matter of the Go code but how you pass it into your program, try passing the path in double quotes "…"

./main -path="/home/spouk/spouk.download/torrent/Сергей Лукьяненко, Собрание сочинений/"
> Flag: -path=`/home/spouk/spouk.download/torrent/Сергей Лукьяненко, Собрание сочинений/`
> flag.Args: []
> Rune: [47 104 111 109 101 47 115 112 111 117 107 47 115 112 111 117 107 46 100 111 119 110 108 111 97 100 47 116 111 114 114 101 110 116 47 1057 1077 1088 1075 1077 1081 32 1051 1091 1082 1100 1103 1085 1077 1085 1082 1086 44 32 1057 1086 1073 1088 1072 1085 1080 1077 32 1089 1086 1095 1080 1085 1077 1085 1080 1081 47]

答案2

得分: 3

这与Go语言无关,也与西里尔字母无关。这是关于shell如何解析命令行的问题。它使用空格来分隔参数。使用双引号(")来覆盖参数。例如,

package main

import (
	"flag"
	"fmt"
	"os"
)

func main() {
	fmt.Printf("os.Args:   %q\n", os.Args[1:])
	wordPtr := flag.String("path", "foo", "a string")
	flag.Parse()
	fmt.Printf("flag.path: %q\n", *wordPtr)
	fmt.Printf("flag.Args: %q\n", flag.Args())
}

输出:

$ go run path.go -path=/home/spouk/Сергей Лукьяненко, Собрание сочинений/
os.Args:   ["-path=/home/spouk/Сергей" "Лукьяненко," "Собрание" "сочинений/"]
flag.path: "/home/spouk/Сергей"
flag.Args: ["Лукьяненко," "Собрание" "сочинений/"]

$ go run path.go -path="/home/spouk/Сергей Лукьяненко, Собрание сочинений/"
os.Args:   ["-path=/home/spouk/Сергей Лукьяненко, Собрание сочинений/"]
flag.path: "/home/spouk/Сергей Лукьяненко, Собрание сочинений/"
flag.Args: []

> Bash参考手册
>
> 3.1.2.3 双引号
>
> 使用双引号(‘"’)将字符括起来可以保留引号内所有字符的字面值[有一些例外]。

英文:

This has nothing to do with Go. This has nothing to do with cyrillic. It is how the shell parses the command line. It is using spaces to separate arguments. Enclose the argument in double quotes (") to override. For example,

package main

import (
	"flag"
	"fmt"
	"os"
)

func main() {
	fmt.Printf("os.Args:   %q\n", os.Args[1:])
	wordPtr := flag.String("path", "foo", "a string")
	flag.Parse()
	fmt.Printf("flag.path: %q\n", *wordPtr)
	fmt.Printf("flag.Args: %q\n", flag.Args())
}

Output:

$ go run path.go -path=/home/spouk/Сергей Лукьяненко, Собрание сочинений/
os.Args:   ["-path=/home/spouk/Сергей" "Лукьяненко," "Собрание" "сочинений/"]
flag.path: "/home/spouk/Сергей"
flag.Args: ["Лукьяненко," "Собрание" "сочинений/"]

$ go run path.go -path="/home/spouk/Сергей Лукьяненко, Собрание сочинений/"
os.Args:   ["-path=/home/spouk/Сергей Лукьяненко, Собрание сочинений/"]
flag.path: "/home/spouk/Сергей Лукьяненко, Собрание сочинений/"
flag.Args: []

> Bash Reference Manual
>
> 3.1.2.3 Double Quotes
>
> Enclosing characters in double quotes (‘"’) preserves the literal
> value of all characters within the quotes [with some exceptions].

huangapple
  • 本文由 发表于 2017年3月6日 11:20:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/42617008.html
匿名

发表评论

匿名网友

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

确定