[C++]cmdline—一个轻量级的C++命令行解析库

#1、说明

cmdline是一个轻量级的c++命令行参数解析工具,全部源码只有一个cmdline.h头文件。

#2、代码

20171210_命令行进行解析.cpp

// 20171210_命令行进行解析.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "cmdline.h"
#include <iostream>

using std::cout;
using std::string;
using std::endl;

int main(int argc, char *argv[])
{
	// 创建一个命令行解析器
	cmdline::parser a;
	// 加入指定类型的输入參数
	// 第一个參数:长名称
	// 第二个參数:短名称(‘\0‘表示没有短名称)
	// 第三个參数:參数描写叙述
	// 第四个參数:bool值,表示该參数是否必须存在(可选。默认值是false)
	// 第五个參数:參数的默认值(可选,当第四个參数为false时该參数有效)
	a.add<string>("host", 'h', "host name", true, "");
	// 第六个參数用来对參数加入额外的限制
	// 这里端口号被限制为必须是1到65535区间的值,通过cmdline::range(1, 65535)进行限制 
	a.add<int>("port", 'p', "port number", true, 80, cmdline::range(1, 65535));
	// cmdline::oneof() 能够用来限制參数的可选值
	a.add<string>("type", 't', "protocol type", false, "http", cmdline::oneof<string>("http", "https", "ssh", "ftp"));
	// 也能够定义bool值
	// 通过调用不带类型的add方法
	a.add("gzip", '\0', "gzip when transfer");
	// 执行解析器
	// 仅仅有当全部的參数都有效时他才会返回
	//  假设有无效參数,解析器会输出错误消息。然后退出程序
	// 假设有‘--help‘或-?

	//这种帮助标识被指定,解析器输出帮助信息。然后退出程序
	a.parse_check(argc, argv);
	// 获取输入的參数值
	cout << a.get<string>("type") << "://"
		<< a.get<string>("host") << ":"
		<< a.get<int>("port") << endl;
	// bool值能够通过调用exsit()方法来推断
	if (a.exist("gzip")) cout << "gzip" << endl;

	return 0;
}


cmdline.h

/*
Copyright (c) 2009, Hideyuki Tanaka
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY <copyright holder> ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#pragma once

#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <string>
#include <stdexcept>
#include <typeinfo>
#include <cstring>
#include <algorithm>
//当编译器非gcc时,不包含cxxabi.h头文件
#ifdef __GNUC__
#include <cxxabi.h>
#endif
#include <cstdlib>

namespace cmdline {

	namespace detail {

		template <typename Target, typename Source, bool Same>
		class lexical_cast_t {
		public:
			static Target cast(const Source &arg) {
				Target ret;
				std::stringstream ss;
				if (!(ss << arg && ss >> ret && ss.eof()))
					throw std::bad_cast();

				return ret;
			}
		};

		template <typename Target, typename Source>
		class lexical_cast_t<Target, Source, true> {
		public:
			static Target cast(const Source &arg) {
				return arg;
			}
		};

		template <typename Source>
		class lexical_cast_t<std::string, Source, false> {
		public:
			static std::string cast(const Source &arg) {
				std::ostringstream ss;
				ss << arg;
				return ss.str();
			}
		};

		template <typename Target>
		class lexical_cast_t<Target, std::string, false> {
		public:
			static Target cast(const std::string &arg) {
				Target ret;
				std::istringstream ss(arg);
				if (!(ss >> ret && ss.eof()))
					throw std::bad_cast();
				return ret;
			}
		};

		template <typename T1, typename T2>
		struct is_
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值