llvm 命令行处理

CommandLine

#include "llvm/Support/CommandLine.h"
#include <iostream>
using namespace std;
using namespace llvm;
char *test_string = "test string";
cl::opt<string> OutputFilename("o", cl::desc("Specify output filename"),
                               cl::value_desc("filename"));
cl::opt<string> InputFilename(cl::Positional, cl::desc("input filename"),
                              cl::Required);
int main(int argc, char *argv[]) {
  cl::ParseCommandLineOptions(argc, argv, test_string);
  std::cout << OutputFilename.c_str() << std::endl;
  std::cout << InputFilename.c_str() << std::endl;
  return 0;
}
$./learn-llvm        
learn-llvm: Not enough positional command line arguments specified!
Must specify at least 1 positional argument: See: ./learn-llvm --help

cl::Positional

使用这个选型,就可以根据参数的位置来获取参数,而不用使用- 这个标志来提示获取参数。

cl::Required

如果设置了这个参数在全局变量中,那么在运行程序的时候必须指定参数。

$./learn-llvm world -o hello                                                                              
hello
world

cl::init

#include "llvm/Support/CommandLine.h"
#include <iostream>
using namespace std;
using namespace llvm;
char *test_string = "test string";
cl::opt<string> OutputFilename("o", cl::desc("Specify output filename"),
                               cl::value_desc("filename"));
cl::opt<string> InputFilename(cl::Positional, cl::desc("input filename"),
                              cl::init("hello"));
int main(int argc, char *argv[]) {
  cl::ParseCommandLineOptions(argc, argv, test_string);
  std::cout << OutputFilename.c_str() << std::endl;
  std::cout << InputFilename.c_str() << std::endl;
  return 0;
}

因为我们在InputFilename中指定了默认的初始化字符串是hello,所以会默认使用hello进行初始化。

./learn-llvm -o world                                                                                    
world
hello

-help

-help选项是linecommand内置的选型。

 $./learn-llvm -help                                                                                       ok 
OVERVIEW: test string
USAGE: learn-llvm [options] input filename

OPTIONS:

Color Options:

  --color       - Use colors in output (default=autodetect)

General options:

  -o=<filename> - Specify output filename

Generic Options:

  --help        - Display available options (--help-hidden for more)
  --help-list   - Display list of available options (--help-list-hidden for more)
  --version     - Display the version of this program
                                                               

我们可以在-o那里看到对-o这个选型的一些描述。

另外一个例子

#include "llvm/Support/CommandLine.h"
using namespace std;
using namespace llvm;

cl::opt<bool> isEMITLLVM("emit-llvm", cl::init(false));
int main(int argc, char *argv[]) {
  cl::ParseCommandLineOptions(argc, argv);
  if (isEMITLLVM.getValue()) {
    llvm::outs() << "emit-llvm" << '\n';
  }
  return 0;
}

代码运行结果

$./learn-llvm -emit-llvm                                                                                  ok 
emit-llvm

如果这里没有输入-emit-llvm ,就不会输出emit-llvm.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值