首页 \ 问答 \ 如何使用NDesk.Options强制执行必需的命令行选项?(How to enforce required command-line options with NDesk.Options?)

如何使用NDesk.Options强制执行必需的命令行选项?(How to enforce required command-line options with NDesk.Options?)

我只是编写一个控制台实用程序,并决定使用NDesk.Options进行命令行解析。 我的问题是,我如何执行必需的命令行选项?

我在文档中看到:

具有所需值的选项(将'='附加到选项名称)或可选值(将选项名称附加':')。

但是,当我在选项名称的末尾加上a =时,行为没有区别。 理想情况下,Parse方法会抛出异常。

还有什么我需要做的吗?

这是我的测试代码:

class Program
{
    static void Main(string[] args)
    {
        bool show_help = false;
        string someoption = null;

        var p = new OptionSet() {
            { "someoption=", "Some String Option", v => someoption = v},
            { "h|help",  "show this message and exit", v => show_help = v != null }
        };

        List<string> extra;
        try
        {
            extra = p.Parse(args);
        }
        catch (OptionException e)
        {
            System.Console.Write("myconsole: ");
            System.Console.WriteLine(e.Message);
            System.Console.WriteLine("Try `myconsole --help' for more information.");
            return;
        }

        if (show_help)
        {
            ShowHelp(p);
            return;
        }

        System.Console.WriteLine("==================");
        System.Console.WriteLine(someoption);
    }

    static void ShowHelp(OptionSet p)
    {
        System.Console.WriteLine("Usage: myconsole [OPTIONS]");
        System.Console.WriteLine();
        System.Console.WriteLine("Options:");
        p.WriteOptionDescriptions(System.Console.Out);
    }
}

I was just writing a console utility and decided to use NDesk.Options for command-line parsing. My question is, How do I enforce required command-line options?

I see in the docs that:

options with a required value (append '=' to the option name) or an optional value (append ':' to the option name).

However, when I put a = at the end of the option name there is no difference in behavior. Ideally the Parse method would throw an exception.

Is there something else I need to do?

Here is my test code:

class Program
{
    static void Main(string[] args)
    {
        bool show_help = false;
        string someoption = null;

        var p = new OptionSet() {
            { "someoption=", "Some String Option", v => someoption = v},
            { "h|help",  "show this message and exit", v => show_help = v != null }
        };

        List<string> extra;
        try
        {
            extra = p.Parse(args);
        }
        catch (OptionException e)
        {
            System.Console.Write("myconsole: ");
            System.Console.WriteLine(e.Message);
            System.Console.WriteLine("Try `myconsole --help' for more information.");
            return;
        }

        if (show_help)
        {
            ShowHelp(p);
            return;
        }

        System.Console.WriteLine("==================");
        System.Console.WriteLine(someoption);
    }

    static void ShowHelp(OptionSet p)
    {
        System.Console.WriteLine("Usage: myconsole [OPTIONS]");
        System.Console.WriteLine();
        System.Console.WriteLine("Options:");
        p.WriteOptionDescriptions(System.Console.Out);
    }
}

原文:https://stackoverflow.com/questions/3427306
更新时间:2022-03-17 22:03

最满意答案

进入文件夹,然后rpm -i 文件名,就行了,不过rpm包的依赖关系解决的没有deb的好,很可能不能够一次成功安装,还需要装其他的东西

其他回答

如果你linuxqq-v1.0.2-beta1.i386.rpm在usr目录下,

就敲cd usr就可以了,然后再敲

rpm -ivh linuxqq-v1.0.2-beta1.i386.rpm执行安装就可以了。

相关问答

更多
  • linux 里安装ISO文件就像windows一样,需要先加载iso里的文件,然后运行里面的文件。不同的是windows需要用虚拟光驱等软件加载ISO文件;Linux有命令直接加载ISO文件 1、将iso文件拷贝到linux系统里,比如放到/opt下 2、挂载ISO文件:mount -o loop /opt/a.iso /mnt/iso (后面为挂载目录,可自行创建) 3、cd /mnt/iso 即可运行iso里的文件。
  • 电脑联网。切换到那个安装包的目录,然后yum执行这个安装包。到时候缺少的文件自动下载
  • 这东西在 mysql 的 devel 包里面。 include 应该直接 #include 或者 就行了。默认 gcc 回去 /usr/include 里面对应的找。
  • rpm格式中red hat中用的,ubuntu是基于debian的系统用的tar的就行,另外安装软件,ubuntu有个软件中心,你进去手搜索一下jdk,双击下,自动安装上。手动可以 sudo apt-get install sun-java6-jdk,就自动连接网络安装好了。
  • 其实很简单的用命令 rpm 安装就可以了 完整的是 rpm -ivh --prefix=[路径] [安装包名称] [路径]处你写上你要安装到的目录就可以来了
  • 首先要明确一点:rpm包,用于redhat、centos、fedora以及Oracle Enterprise Linux等发行版。如果是ubuntu、debian是不能用rpm的,要用deb包。所以,先检查一下自己下载的安装包合不合适。要是不合适,赶快换。 如果说确信没错。rpm包的安装是rpm -i,然后是空格,空格的后面紧跟着你要安装的那个rpm包的完整的名称。 就是rpm -i xxxx.rpm。 还有两个参数,可写可不写。一个是v,有它,电脑会提示安装成功与否。一个是h,所谓h是hash,就是井字号 ...
  • 你可以在命令行下进行安装 cd linuxqq-v1.0.2-beta1.i386.rpm所在目录 rpm -ivh linuxqq-v1.0.2-beta1.i386.rpm就OK了 前提是,你的Linux版本支持这个包啊。
  • 你man rpm看看嘛,肯定要在rpm包同路径下操作,要不就写出路径
  • 进入文件夹,然后rpm -i 文件名,就行了,不过rpm包的依赖关系解决的没有deb的好,很可能不能够一次成功安装,还需要装其他的东西
  • 如果你linuxqq-v1.0.2-beta1.i386.rpm在usr目录下, 就敲cd usr就可以了,然后再敲 rpm -ivh linuxqq-v1.0.2-beta1.i386.rpm执行安装就可以了。

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)