【问题标题】:Command line parsing lib VLang命令行解析 lib VLang
【发布时间】:2019-12-05 02:23:52
【问题描述】:

我想用V programming language 编写一个命令行应用程序。有命令行解析的库吗?

【问题讨论】:

    标签: vlang


    【解决方案1】:

    是的,操作系统模块。此示例输出每个命令行参数

    import os
    
    fn main() {
        for arg in os.args {
            println(arg)
        }
    }
    

    在我的系统上运行时:program.exe hello! 返回

    D:\Documents\V\program.exe
    hello!
    

    编辑:我现在看到了您使用命令行解析的目标。不,没有允许您执行此操作的现有模块。

    【讨论】:

    • 我不会认为os 是一个用于命令行解析的库。我正在寻找类似的东西,例如Python 的Click.
    【解决方案2】:

    您可以使用官方的flag 模块。 参见例如: https://github.com/vlang/v/blob/689003454b5c60fa13a6a0b44c39f79847806609/tools/performance_compare.v#L204

    从用户的角度来看,这会产生类似的结果:

    0[13:29:12] /v/nv $ tools/performance_compare --help
    performance_compare 0.0.4
    -----------------------------------------------
    Usage: performance_compare [options] COMMIT_BEFORE [COMMIT_AFTER]
    
    Description:
      Compares V executable size and performance,
      between 2 commits from V's local git history.
      When only one commit is given, it is compared to master.
    
    The arguments should be at least 1 and at most 2 in number.
    
    Options:
      --help <bool>:false       Show this help screen
    
      --vcrepo <string>:https://github.com/vlang/vc
                                The url of the vc repository. You can clone it
                                beforehand, and then just give the local folder
                                path here. That will eliminate the network ops
                                done by this tool, which is useful, if you want
                                to script it/run it in a restrictive vps/docker.
    
      --verbose <bool>:false    Be more verbose
    
      --hyperfine_options <string>:
                                Additional options passed to hyperfine.
                                For example on linux, you may want to pass:
                                   --hyperfine_options "--prepare 'sync; echo 3 | sudo tee /proc/sys/vm/drop_caches'"
    
      --workdir <string>:/tmp   A writable folder, where the comparison will be done.
    
    0[13:29:13] /v/nv $
    

    【讨论】:

      【解决方案3】:

      截至 2020 年 11 月,命令行参数解析包含在标准库中。

      Example from V repository

      import cli { Command, Flag }
      import os
      
      fn main() {
          mut cmd := Command{
              name: 'cli'
              description: 'An example of the cli library.'
              version: '1.0.0'
          }
          mut greet_cmd := Command{
              name: 'greet'
              description: 'Prints greeting in different languages.'
              usage: '<name>'
              required_args: 1
              pre_execute: greet_pre_func
              execute: greet_func
              post_execute: greet_post_func
          }
          greet_cmd.add_flag(Flag{
              flag: .string
              required: true
              name: 'language'
              abbrev: 'l'
              description: 'Language of the message.'
          })
          greet_cmd.add_flag(Flag{
              flag: .int
              name: 'times'
              value: '3'
              description: 'Number of times the message gets printed.'
          })
          cmd.add_command(greet_cmd)
          cmd.parse(os.args)
      }
      

      输出

      $ v run ./examples/cli.v 
      Usage: cli [flags] [commands]
      
      An example of the cli library.
      
      Flags:
        -help               Prints help information.
        -version            Prints version information.
      
      Commands:
        greet               Prints greeting in different languages.
        help                Prints help information.
        version             Prints version information.
      

      【讨论】:

        【解决方案4】:

        nedpals/vargs 库。

        【讨论】:

          【解决方案5】:

          本地:

          import os
          import flag
          
          
          
          const (
              tool_version = '0.0.4'
          )
          
          
          mut fp := flag.new_flag_parser(os.args)
          fp.version( tool_version )
          

          使用 v-args 库:

          import vargs // or import nedpals.vargs for vpm users
          import os
          
          fn main() {
              // Second argument removes the first argument which contains the path of the executable.
              mut _args := vargs.new(os.args, 1)
              
              // Use the `alias` method if you want to map an option to an existing option.
              _args.alias('W', 'with')
          
              // Parsing is now a separate step
              _args.parse()
          
              println(_args.str())
              println(_args.command)
              println(_args.unknown[0])
              println('with? ' + _args.options['with'])
          }
          

          【讨论】:

            猜你喜欢
            • 2010-11-27
            • 1970-01-01
            • 2013-11-18
            • 2012-05-09
            • 2013-01-13
            • 1970-01-01
            • 2013-03-21
            • 2016-03-30
            相关资源
            最近更新 更多