189 8069 5689

go语言命令行参数 go语言执行cmd命令

写命令行应用程序什么不可或缺?Go可以这样处理命令行参数

Go语言内置的flag包实现了命令行参数的解析,flag包使得开发命令行工具更为简单。

创新互联从2013年成立,是专业互联网技术服务公司,拥有项目做网站、成都做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元无为做网站,已为上家服务,为无为各地企业和个人服务,联系电话:18980820575

如果你只是简单的想要获取命令行参数,可以像下面隐罩桥的代码示例一样使用os.Args来获取命令行参数。

将上面的代码执行go build -o "args_demo"编译之后,执行:

os.Args是一个存储命令行参数的字符串切片,它的第一个元素是执行文件的名称。

本文介绍了flag包的常用函数和基本用法,更详细的内容请查看官方文档。

flag包支持的命令行参数类型有bool、int、int64、uint、uint64、float float64、string、duration。

有以下两种常用的定义命令行flag参数的方法。

基本格式如下:

flag.Type(flag名, 默认值, 帮助信息)*Type 例如我们要定义姓名、年龄、婚否三个命令行参数,我们灶猛可以按如下方式定义:

需要注意的是,此时name、age、married、delay均为对应类型的指针。

基本格式如下: flag.TypeVar(Type指针, flag名, 默认值, 帮助信息) 例如我们要定义姓名、年龄、婚否三个命令行参数,我们可以按如下方式定义:

通过以上两种方法定义好命令行flag参数后,需要通过调用flag.Parse()来对命令行参数进行解析。

支持的命令行参数格式有以下几种:

其中,布尔类型的参数必须使用等号的方式指定。

Flag解析在第一个非flag参数(单个”-“不是flag参数)之前停止,或者在终止符”–“之闷祥后停止。

定义

使用

命令行参数使用提示:

$ ./flag_demo -help

Usage of ./flag_demo:

-age int

年龄 (default 18)

-d duration

时间间隔

-married

婚否

-name string

姓名 (default "张三")

正常使用命令行flag参数:

使用非flag命令行参数:

原文链接:

27.Go 解析命令行参数

标准库中的flag包用于解析命令行参数:

上面的输出是调用go run $ file -echo echo-arg Additional arg的结果。

定兄李义参侍陆数

假设你的程序有一个-retries整数选项。

你可以使用以下标志包注册此类选项:

还有其他常见类型的功能:

POSIX variant --retries or Windows variant /retries are not recognized.

For boolean values you can say: -help (implicitly true), -help=true or -help=false.

-help false is not a valid form for boolean variables.

Parsing and accessing remaining arguments

After parsing arguments, call flag.Parse().

Parsing fails if:

unknown flag was given on command-line

a flag didn’t parse based on its type (e.g. it was registered as int but the value was not a valid number)

In case of failure, help text describing flags is shown and program exits with error code 2.

You can explicitly print help text using flag.Usage(). This is often triggered by -help flag.

Help text is based on usage text provided in flag.IntVar and others.

Command-line arguments that don’t start with - are untouched and can be accessed with flag.Args().

flag包羡谈迟缺少的功能:

no support for POSIX style --name, only -name is supported

no support for short alternatives e.g. -n being synonym with --name

no suport for Windows style /name

If you need those features, your options are:

access raw cmd-line arguments

use a third party library

If flag package or a third-party library doesn’t provide the features you want, you can parse the arguments yourself.

The above output is a result of go run $file -echo echo-arg additional arg.

Raw command-line arguments can be accessed via []string slice os.Args.

First element is name of the executable.

Remaining elements are cmd-line arguments as decoded by OS shell.

On Windows cmd-line arguments are a single UTF-16 Unicode string.

Go runtime converts them to UTF-8 string and splits into separate arguments to unify handling across different operating systems.

Functionaly provided by standard library package flag is limited.

其他提供了命令行参数解析的库:

Go执行命令行操作

go中提供了 os/exec 包使用 Command 函数可友颂以完成一些调用命令行的操作。因为系统的不同,好悄郑调用的参数会有点些不一样。

其他的创建执行都是一样的。

我们可以封装一个函数,输入需要执行的命令,输入返回结果

调运凯用测试

如何在Go语言中使用flag包对命令行进行参数解析

flag 是Go 标准库提供的解析改滚命令行孝袭参数的包。

使用方式:

flag.Type(name, defValue, usage)

其中Type为String, Int, Bool等;并返回一个相应类型的指针。

flag.TypeVar(flagvar, name, defValue, usage)

将flag绑定到一个变量上。核慎余


当前题目:go语言命令行参数 go语言执行cmd命令
本文地址:http://cdxtjz.cn/article/ddpgihh.html

其他资讯