标签: getopt

无法识别的选项错误:shell 中的 getopt 命令

我是 shell 和 Linux 的新手,如果有人能帮助我找出命令中的错误,那就太好了:

if ! options=$(getopt -n myscript -l a:,b:,cc:,dd:,ee:,ff:,gg:,hh: -- "$@"); then exit 1; fi
Run Code Online (Sandbox Code Playgroud)

我收到一条错误消息:

mhagent: unrecognized option '--hh'
options=' --aa '\''val1'\'' --ibb '\''val2'\'' --cc '\''val4'\'' --dd '\''val4'\'' --ee '\''val5'\'' --ff '\''val6'\'' --gg '\''val7'\'' --'
Run Code Online (Sandbox Code Playgroud)

如果我删除最后一个选项:hh,它工作正常。

if ! options=$(getopt -n myscript -l a:,b:,cc:,dd:,ee:,ff:,gg: -- "$@"); then exit 1; fi
Run Code Online (Sandbox Code Playgroud)

bash shell getopt

3
推荐指数
1
解决办法
6145
查看次数

使用不带参数的 getopts 来获取帮助输出

您好,我正在创建一个使用 getopts 的 bash 脚本。现在我想创建一个“-h”参数来获取帮助。但每次我都必须给参数一个参数。

Now

test.sh -h test

What I want

test.sh -h
help
help
help



while getopts :c:s:d:h:I:p:r FLAG; do
  case $FLAG in


        s)
                SOURCE=$OPTARG
                ;;
        d)
                DESTINATION=$OPTARG
                ;;
        I)
                ISSUE=$OPTARG
                ;;
        c)
                CUSTOMER=$OPTARG
                test -e /etc/squid3/conf.d/$CUSTOMER.conf
                customer_available=$?
                ;;
        p)
                PORT=$OPTARG
                ;;
        h)      HELP=$OPTARG
                echo help
Run Code Online (Sandbox Code Playgroud)

bash arguments manpage getopt getopts

3
推荐指数
1
解决办法
7683
查看次数

getopt 中的 opterr 声明

以下是来自http://www.gnu.org的示例代码。大多数人肯定会看到,它是 getopt,我对变量声明有疑问。为什么前面没有写任何类型或任何东西

opterr = 0;
Run Code Online (Sandbox Code Playgroud)

我以前从未见过这样的情况。

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main (int argc, char **argv)
{
  int aflag = 0;
  int bflag = 0;
  char *cvalue = NULL;
  int index;
  int c;

  opterr = 0;


  while ((c = getopt (argc, argv, "abc:")) != -1)
    switch (c)
      {
      case 'a':
        aflag = 1;
        break;
      case 'b':
        bflag = 1;
        break;
      case 'c':
        cvalue = optarg;
        break;
      case '?':
        if (optopt == 'c')
          fprintf (stderr, …
Run Code Online (Sandbox Code Playgroud)

c getopt

3
推荐指数
1
解决办法
2829
查看次数

为什么 C getopt_long_only() 不为未知选项设置 optopt?

getopt_long_only()我正在尝试与自定义错误消息一起使用。代码如下所示。我尝试设置 opterr=0 并在 optstring 开头使用冒号来禁用内置错误消息。我添加了一段用布尔值控制的代码块optoptWorks = true来尝试自定义错误消息,例如在-z使用错误选项时打印消息。但是optopt始终设置为 0 并且?我使用的错误消息没有意义。':'(冒号大小写)错误(缺少参数,例如-d)对于自定义消息确实可以正常工作。关闭内置错误消息并进行处理?似乎会导致optopt始终设置为 0,因此我无法打印有问题的选项 ( -z is not recognized)。我在 Debian Linux gcc 4.9.4 和 Cygwin gcc 7.3.0 上编译,两者都给出相同的结果。看起来getopt_long_only()可能设置不optopt正确或者我错过了什么?网络上的许多示例通过使用内置错误消息或仅打印用法而不告诉用户哪个选项无法识别来解决此问题。

这是输出optoptWorks=false

$ ./testoptget -z
testoptget: unknown option -- z

-d #            Set the debug level.
-h, --help      Print program usage.
-q              Run in quiet mode (log messages to syslog but not console).
-v, --version …
Run Code Online (Sandbox Code Playgroud)

c getopt

3
推荐指数
1
解决办法
2579
查看次数

将optarg作为C++字符串对象获取

我使用getopt_long来处理C++应用程序中的命令行参数.这些示例都显示printf("Username: %s\n", optarg)了处理示例中的内容.这非常适合展示一个示例,但我希望能够实际存储这些值以供以后使用.其余大部分代码都使用string对象而不是char*我需要将optarg的内容转换/复制到字符串中.

string bar;
while(1) {
    c = getopt_long (argc, argv, "s:U:", long_options, &option_index);
    if (c == -1) break;
    switch(c)
        {
            case 'U':
                // What do I need to do here to get
                // the value of optarg into the string
                // object bar?
                bar.assign(optarg);
                break;
        }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码编译,但是当它执行时我得到一个Illegal instruction错误,如果我尝试使用printf打印出bar的值(它似乎对cout工作正常).

// Runs just fine, although I'm not certain it is actually safe!
cout << " bar: " << bar << "\n"; …
Run Code Online (Sandbox Code Playgroud)

c++ string getopt getopt-long

2
推荐指数
2
解决办法
6637
查看次数

如何多次调用bash中的getopts

我有一个公共库,我使用几个解析命令行选项的脚本,但是我也希望我的个人脚本能够处理参数...例如

common.sh:

function get_options {
    echo -e "in getoptions"
    echo $OPTIND
    while getopts ":ab:" optionName; do
       [ ... processing code ... ]
    done
}
Run Code Online (Sandbox Code Playgroud)

. ./common.sh

function get_local_options {
    echo -e "in getoptions"
    echo $OPTIND
    while getopts ":xy:" optionName; do
       [ ... processing code ... ]
    done
}

get_local_options $*
OPTIND=1
get_options $*
Run Code Online (Sandbox Code Playgroud)

问题是,如果我用a调用a.sh:

a.sh -x -y foo -a -b bar
Run Code Online (Sandbox Code Playgroud)

get_options停止在"foo"处理,因为它停在第一个"非选项"

没有改写自己的东西吗?

bash getopt

2
推荐指数
1
解决办法
4129
查看次数

为什么getopt在我的mac os中运行不正常?

在bash:我输入命令:

getopt -l name,data -- --namd
Run Code Online (Sandbox Code Playgroud)

而输出是

-- name,pp -- --namd
Run Code Online (Sandbox Code Playgroud)

输入:

getopt -l name,data -- --name
Run Code Online (Sandbox Code Playgroud)

输出其他

-- name,pp -- --name
Run Code Online (Sandbox Code Playgroud)

我输入时为什么不告诉我错误getopt -l name,data -- --namd

shell getopt

2
推荐指数
1
解决办法
4291
查看次数

如何让Getopt :: Long + pod2usage工作?

我准备好了,所以我在这里:)

我正在尝试为我的Perl程序制作文档,但我从来没有设法让Getopt :: Long和pod2man工作.

这是我为测试目的编写的一个简单程序:

#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;

Getopt::Long::Configure ("bundling");

my $option = "";
my $verbose = 1;
my $quiet = 0;
my $man = 0;
my $help = 0;

GetOptions (
        "option=s" => \$option,
        "verbose" => sub { $verbose = 1; $quiet = 0 },
        "quiet|noverbose" => sub { $verbose = 0; $quiet = 1 },
        "help|?" => \$help,
        man => \$man
) or pod2usage("Error in command line.\n");

pod2usage(1) if $help;
pod2usage(-exitval => …
Run Code Online (Sandbox Code Playgroud)

perl manual getopt

2
推荐指数
1
解决办法
1383
查看次数

'main'通常是一个非静态函数ERROR

出现两个错误:main.c:80:警告:'main'通常是一个非静态函数main.c:88:错误:输入结束时预期的声明或语句我似乎无法找到问题......花括号的数量相等......似乎有什么问题?

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include "main-getopt.h"


void print_usage_and_abort( const char *message )
{
    if( NULL != message )
        fprintf( stderr, "Error: %s\n", message );

    fprintf( stderr, "Usage: partitioner -n <nodes> [ -f <basename> ]\n\n" );
    exit( -1 );
}

void parsing (int argc, char **argv, struct Params *params)
{
    char error_message[256];

    params->nodes = 0;
    memcpy( params->filename_base, "output", strlen("output") + 1 );

    int opt;
    size_t len;
    int numarg;

    while ((opt = getopt(argc, argv, "n:f:")) …
Run Code Online (Sandbox Code Playgroud)

c linker getopt

2
推荐指数
1
解决办法
2万
查看次数

如何在没有无效选项警告的情况下为Vagrant实现自定义选项?

我正在尝试为Vagrant实现新的自定义选项,如下所示Vagrantfile:

# -*- mode: ruby -*-
require 'getoptlong'

opts = GetoptLong.new(
  [ '--vm-name',        GetoptLong::OPTIONAL_ARGUMENT ],
)

vm_name        = ENV['VM_NAME'] || 'default'

begin
  opts.each do |opt, arg|
    case opt
      when '--vm-name';        vm_name        = arg
    end
  end
  rescue
# @fixme: An invalid option error happens here.
end

Vagrant.configure(2) do |config|
  config.vm.define vm_name
  config.vm.provider "virtualbox" do |vbox, override|
    override.vm.box = "ubuntu/wily64"
  end
end
Run Code Online (Sandbox Code Playgroud)

现在,每当我运行任何流浪汉命令时,它都会显示以下警告,例如

vagrant destroy -f
Run Code Online (Sandbox Code Playgroud)

/opt/vagrant/embedded/gems/gems/vagrant-1.8.1/bin/vagrant:无效选项 - f

另一个例子:

$ vagrant --vm-name=foo up --no-provision
/opt/vagrant/embedded/gems/gems/vagrant-1.8.1/bin/vagrant: unrecognized option `--no-provision'
Bringing …
Run Code Online (Sandbox Code Playgroud)

ruby getopt rescue vagrant vagrantfile

2
推荐指数
1
解决办法
775
查看次数

标签 统计

getopt ×10

bash ×3

c ×3

shell ×2

arguments ×1

c++ ×1

getopt-long ×1

getopts ×1

linker ×1

manpage ×1

manual ×1

perl ×1

rescue ×1

ruby ×1

string ×1

vagrant ×1

vagrantfile ×1