Compare commits
	
		
			12 Commits
		
	
	
		
			ce4d0fc423
			...
			main
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 5879273771 | |||
| f58d83b4ee | |||
| 85a219126d | |||
| 1345a0514e | |||
| 0a7179380a | |||
| 56a50201fe | |||
| 7b2acabfd6 | |||
| ad655041dd | |||
| e9dd39f53b | |||
| cffe8bfe4a | |||
| b4663a01e8 | |||
| b1eaae793e | 
@@ -34,7 +34,7 @@ conan create .
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## 示例程序
 | 
					## 示例程序
 | 
				
			||||||
构建简单的命令行程序
 | 
					构建简单的命令行程序,更多示例请参考`example`目录
 | 
				
			||||||
```c
 | 
					```c
 | 
				
			||||||
#include "ArgParse.h"
 | 
					#include "ArgParse.h"
 | 
				
			||||||
#include <stdbool.h>
 | 
					#include <stdbool.h>
 | 
				
			||||||
@@ -42,11 +42,11 @@ conan create .
 | 
				
			|||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ArgParse *Init() {
 | 
					ArgParse *Init() {
 | 
				
			||||||
    ArgParse *ap = argParseInit("简单的命令行工具示例");
 | 
					    ArgParse *ap = argParseInit("简单的命令行工具示例",ArgParseNOVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // 添加第一个命令
 | 
					    // 添加第一个命令
 | 
				
			||||||
    Command *cmd = argParseAddCommand(
 | 
					    Command *cmd = argParseAddCommand(
 | 
				
			||||||
        ap, "list", "列出文件列表", NULL, NULL, NULL, SINGLEVALUE);
 | 
					        ap, "list", "列出文件列表", NULL, NULL, NULL, ArgParseSINGLEVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // 添加第一个命令的参数
 | 
					    // 添加第一个命令的参数
 | 
				
			||||||
    argParseAddArg(cmd,
 | 
					    argParseAddArg(cmd,
 | 
				
			||||||
@@ -56,7 +56,7 @@ ArgParse *Init() {
 | 
				
			|||||||
                   NULL,
 | 
					                   NULL,
 | 
				
			||||||
                   NULL,
 | 
					                   NULL,
 | 
				
			||||||
                   false,
 | 
					                   false,
 | 
				
			||||||
                   NOVALUE);
 | 
					                   ArgParseNOVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return ap;
 | 
					    return ap;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,7 +6,7 @@ import os
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class loggingRecipe(ConanFile):
 | 
					class loggingRecipe(ConanFile):
 | 
				
			||||||
    name = "cargparse"
 | 
					    name = "cargparse"
 | 
				
			||||||
    version = "0.2.0"
 | 
					    version = "0.3.2"
 | 
				
			||||||
    license = "MIT"
 | 
					    license = "MIT"
 | 
				
			||||||
    author = "321640253@qq.com"
 | 
					    author = "321640253@qq.com"
 | 
				
			||||||
    url = "https://gitea.youmetme.wang/youmetme/logging"
 | 
					    url = "https://gitea.youmetme.wang/youmetme/logging"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,11 +4,11 @@
 | 
				
			|||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ArgParse *Init() {
 | 
					ArgParse *Init() {
 | 
				
			||||||
    ArgParse *ap = argParseInit("简单的命令行工具示例",NOVALUE);
 | 
					    ArgParse *ap = argParseInit("简单的命令行工具示例",ArgParseNOVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // 添加第一个命令
 | 
					    // 添加第一个命令
 | 
				
			||||||
    Command *cmd = argParseAddCommand(
 | 
					    Command *cmd = argParseAddCommand(
 | 
				
			||||||
        ap, "list", "列出文件列表", NULL, NULL, NULL, SINGLEVALUE);
 | 
					        ap, "list", "列出文件列表", NULL, NULL, NULL, ArgParseSINGLEVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // 添加第一个命令的参数
 | 
					    // 添加第一个命令的参数
 | 
				
			||||||
    argParseAddArg(cmd,
 | 
					    argParseAddArg(cmd,
 | 
				
			||||||
@@ -18,7 +18,7 @@ ArgParse *Init() {
 | 
				
			|||||||
                   NULL,
 | 
					                   NULL,
 | 
				
			||||||
                   NULL,
 | 
					                   NULL,
 | 
				
			||||||
                   false,
 | 
					                   false,
 | 
				
			||||||
                   NOVALUE);
 | 
					                   ArgParseNOVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return ap;
 | 
					    return ap;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,6 +7,12 @@
 | 
				
			|||||||
extern "C" {
 | 
					extern "C" {
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef _MSC_VER
 | 
				
			||||||
 | 
					#define NORETURN __declspec(noreturn)
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
 | 
					#define NORETURN _Noreturn
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define ARG_DEFAULT_HELP_FLAG "--help"
 | 
					#define ARG_DEFAULT_HELP_FLAG "--help"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct ArgParse ArgParse; // 解析器
 | 
					typedef struct ArgParse ArgParse; // 解析器
 | 
				
			||||||
@@ -16,9 +22,9 @@ typedef int (*ArgParseCallback)(ArgParse *argParse,
 | 
				
			|||||||
                                int       val_len); // 回调函数
 | 
					                                int       val_len); // 回调函数
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef enum {
 | 
					typedef enum {
 | 
				
			||||||
    NOVALUE = 0,     // 无值
 | 
					    ArgParseNOVALUE = 0, // 无值
 | 
				
			||||||
    SINGLEVALUE,     // 单值 例如: -i https://www.baidu.com
 | 
					    ArgParseSINGLEVALUE, // 单值 例如: -i https://www.baidu.com
 | 
				
			||||||
    MULTIVALUE,      // 多值 例如: -s a b c 或 -s a -s b -s c等
 | 
					    ArgParseMULTIVALUE,  // 多值 例如: -s a b c 或 -s a -s b -s c等
 | 
				
			||||||
} ArgParseValueType;     // 值类型
 | 
					} ArgParseValueType;     // 值类型
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct CommandArgs {
 | 
					typedef struct CommandArgs {
 | 
				
			||||||
@@ -303,7 +309,7 @@ char *argParseGenerateHelp(ArgParse *argParse);
 | 
				
			|||||||
char *
 | 
					char *
 | 
				
			||||||
argParseGenerateArgErrorMsg(ArgParse *argParse, char *name, bool short_flag);
 | 
					argParseGenerateArgErrorMsg(ArgParse *argParse, char *name, bool short_flag);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
_Noreturn void argParseError(ArgParse   *argParse,
 | 
					NORETURN void argParseError(ArgParse   *argParse,
 | 
				
			||||||
                            Command    *lastCommand,
 | 
					                            Command    *lastCommand,
 | 
				
			||||||
                            const char *prefix,
 | 
					                            const char *prefix,
 | 
				
			||||||
                            const char *suffix);
 | 
					                            const char *suffix);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,6 +7,10 @@
 | 
				
			|||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define RED   "\033[0;31m"
 | 
				
			||||||
 | 
					#define BLUE  "\033[0;34m"
 | 
				
			||||||
 | 
					#define RESET "\033[0m"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static bool _AutoHelp = true; // 是否自动添加帮助信息
 | 
					static bool _AutoHelp = true; // 是否自动添加帮助信息
 | 
				
			||||||
static bool _COLOR    = true; // 是否启用颜色
 | 
					static bool _COLOR    = true; // 是否启用颜色
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -37,15 +41,20 @@ ArgParse *argParseInit(char *documentation, ArgParseValueType value_type) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @brief 自动帮助信息回调函数
 | 
					 * @brief 自动帮助信息回调函数
 | 
				
			||||||
 | 
					 * @param argParse ArgParse结构体指针
 | 
				
			||||||
 | 
					 * @param val 参数值
 | 
				
			||||||
 | 
					 * @param val_len 参数值长度
 | 
				
			||||||
 | 
					 * @return 无返回值,将直接结束程序
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
int __helpCallback(ArgParse *argParse, char **val, int val_len) {
 | 
					NORETURN int __helpCallback(ArgParse *argParse, char **val, int val_len) {
 | 
				
			||||||
    if (argParse == NULL) {
 | 
					    if (argParse == NULL) {
 | 
				
			||||||
        return -1;
 | 
					        exit(1);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    char *help_doc = argParseGenerateHelp(argParse);
 | 
					    char *help_doc = argParseGenerateHelp(argParse);
 | 
				
			||||||
    printf("%s\n", help_doc);
 | 
					    printf("%s\n", help_doc);
 | 
				
			||||||
    free(help_doc);
 | 
					    free(help_doc);
 | 
				
			||||||
    return 0;
 | 
					    argParseFree(argParse);
 | 
				
			||||||
 | 
					    exit(0);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
@@ -67,7 +76,7 @@ void argParseAutoHelp(ArgParse *argParse) {
 | 
				
			|||||||
                         NULL,
 | 
					                         NULL,
 | 
				
			||||||
                         __helpCallback,
 | 
					                         __helpCallback,
 | 
				
			||||||
                         false,
 | 
					                         false,
 | 
				
			||||||
                         NOVALUE);
 | 
					                         ArgParseNOVALUE);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int __commandHelpCallback(ArgParse *argParse, char **val, int val_len) {
 | 
					int __commandHelpCallback(ArgParse *argParse, char **val, int val_len) {
 | 
				
			||||||
@@ -96,7 +105,7 @@ void argParseCommandAutoHelp(Command *command) {
 | 
				
			|||||||
                   NULL,
 | 
					                   NULL,
 | 
				
			||||||
                   __commandHelpCallback,
 | 
					                   __commandHelpCallback,
 | 
				
			||||||
                   false,
 | 
					                   false,
 | 
				
			||||||
                   NOVALUE);
 | 
					                   ArgParseNOVALUE);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Command *argParseAddCommand(ArgParse         *argParse,
 | 
					Command *argParseAddCommand(ArgParse         *argParse,
 | 
				
			||||||
@@ -314,7 +323,7 @@ int __processArgs(ArgParse *argParse, CommandArgs *arg, int arg_index) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    int current_index = arg_index;
 | 
					    int current_index = arg_index;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (arg->value_type == MULTIVALUE) {
 | 
					    if (arg->value_type == ArgParseMULTIVALUE) {
 | 
				
			||||||
        for (int i = arg_index + 1; i < argParse->argc; i++) {
 | 
					        for (int i = arg_index + 1; i < argParse->argc; i++) {
 | 
				
			||||||
            if (checkArgType(argParse->argv[i]) ==
 | 
					            if (checkArgType(argParse->argv[i]) ==
 | 
				
			||||||
                COMMAND) { // COMMAND是无--或-开头的字符串,也可认定为参数值
 | 
					                COMMAND) { // COMMAND是无--或-开头的字符串,也可认定为参数值
 | 
				
			||||||
@@ -325,12 +334,12 @@ int __processArgs(ArgParse *argParse, CommandArgs *arg, int arg_index) {
 | 
				
			|||||||
                break;
 | 
					                break;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    } else if (arg->value_type == SINGLEVALUE) {
 | 
					    } else if (arg->value_type == ArgParseSINGLEVALUE) {
 | 
				
			||||||
        if (arg_index + 1 < argParse->argc) {
 | 
					        if (arg_index + 1 < argParse->argc) {
 | 
				
			||||||
            argParseSetArgVal(arg, argParse->argv[arg_index + 1]);
 | 
					            argParseSetArgVal(arg, argParse->argv[arg_index + 1]);
 | 
				
			||||||
            current_index = arg_index + 1;
 | 
					            current_index = arg_index + 1;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    } else if (arg->value_type == NOVALUE) {
 | 
					    } else if (arg->value_type == ArgParseNOVALUE) {
 | 
				
			||||||
        current_index = arg_index;
 | 
					        current_index = arg_index;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -437,7 +446,7 @@ int __processCommand(ArgParse *argParse, char *name, int command_index) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    command              = argParseFindCommand(argParse, name); // 查找命令
 | 
					    command              = argParseFindCommand(argParse, name); // 查找命令
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (command == NULL && argParse->value_type == NOVALUE) {
 | 
					    if (command == NULL && argParse->value_type == ArgParseNOVALUE) {
 | 
				
			||||||
        char *msg = NULL;
 | 
					        char *msg = NULL;
 | 
				
			||||||
        if (name != NULL) {
 | 
					        if (name != NULL) {
 | 
				
			||||||
            msg = stringNewCopy("\033[1;31mERROR\033[0m:");
 | 
					            msg = stringNewCopy("\033[1;31mERROR\033[0m:");
 | 
				
			||||||
@@ -449,7 +458,7 @@ int __processCommand(ArgParse *argParse, char *name, int command_index) {
 | 
				
			|||||||
        return -1;
 | 
					        return -1;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (command == NULL && argParse->value_type != NOVALUE) {
 | 
					    if (command == NULL && argParse->value_type != ArgParseNOVALUE) {
 | 
				
			||||||
        return __processVal(argParse, command_index);
 | 
					        return __processVal(argParse, command_index);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -463,7 +472,7 @@ int __processCommand(ArgParse *argParse, char *name, int command_index) {
 | 
				
			|||||||
        switch (argType) {
 | 
					        switch (argType) {
 | 
				
			||||||
        case COMMAND: {
 | 
					        case COMMAND: {
 | 
				
			||||||
            // 命令无值则处理子命令
 | 
					            // 命令无值则处理子命令
 | 
				
			||||||
            if (command->value_type == NOVALUE) {
 | 
					            if (command->value_type == ArgParseNOVALUE) {
 | 
				
			||||||
                __processSubCommand(argParse, command, argParse->argv[i], i);
 | 
					                __processSubCommand(argParse, command, argParse->argv[i], i);
 | 
				
			||||||
                return argParse->argc - 1;
 | 
					                return argParse->argc - 1;
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
@@ -523,6 +532,7 @@ int __processCommand(ArgParse *argParse, char *name, int command_index) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * @brief 解析命令行参数
 | 
					 * @brief 解析命令行参数
 | 
				
			||||||
 | 
					 * @errors: 错误信息字符串统一又调用方申请,处理函数释放
 | 
				
			||||||
 * @param argParse 解析器指针
 | 
					 * @param argParse 解析器指针
 | 
				
			||||||
 * @param argc 参数个数
 | 
					 * @param argc 参数个数
 | 
				
			||||||
 * @param argv 参数列表
 | 
					 * @param argv 参数列表
 | 
				
			||||||
@@ -574,6 +584,47 @@ void argParseParse(ArgParse *argParse, int argc, char *argv[]) {
 | 
				
			|||||||
                                            argParse->current_command->val,
 | 
					                                            argParse->current_command->val,
 | 
				
			||||||
                                            argParse->current_command->val_len);
 | 
					                                            argParse->current_command->val_len);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // 检查全局参数必填参数是否已设置
 | 
				
			||||||
 | 
					    for (int i = 0; i < argParse->global_args_len; i++) {
 | 
				
			||||||
 | 
					        if (argParse->global_args[i]->required &&
 | 
				
			||||||
 | 
					            argParse->global_args[i]->is_trigged == false) {
 | 
				
			||||||
 | 
					            // 错误处理,必填全局参数未设置
 | 
				
			||||||
 | 
					            char *msg =
 | 
				
			||||||
 | 
					                stringNewCopy(RED "ERROR" RESET ": Global Option " BLUE);
 | 
				
			||||||
 | 
					            if (argParse->global_args[i]->short_opt != NULL) {
 | 
				
			||||||
 | 
					                __catStr(&msg, 1, argParse->global_args[i]->short_opt);
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                __catStr(&msg, 1, argParse->global_args[i]->long_opt);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            __catStr(&msg, 1, RESET " is required");
 | 
				
			||||||
 | 
					            argParseError(argParse, NULL, msg,
 | 
				
			||||||
 | 
					                          NULL); // 错误处理
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // 检查当前命令的必填参数是否已设置
 | 
				
			||||||
 | 
					    if (argParse->current_command != NULL) {
 | 
				
			||||||
 | 
					        for (int i = 0; i < argParse->current_command->args_len; i++) {
 | 
				
			||||||
 | 
					            if (argParse->current_command->args[i]->required &&
 | 
				
			||||||
 | 
					                argParse->current_command->args[i]->is_trigged == false) {
 | 
				
			||||||
 | 
					                // 错误处理,必填参数未设置
 | 
				
			||||||
 | 
					                char *msg = stringNewCopy(RED "ERROR" RESET ": Command " BLUE);
 | 
				
			||||||
 | 
					                __catStr(&msg, 1, argParse->current_command->name);
 | 
				
			||||||
 | 
					                __catStr(&msg, 1, RESET " Option " BLUE);
 | 
				
			||||||
 | 
					                if (argParse->current_command->args[i]->short_opt != NULL) {
 | 
				
			||||||
 | 
					                    __catStr(
 | 
				
			||||||
 | 
					                        &msg, 1, argParse->current_command->args[i]->short_opt);
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                    __catStr(
 | 
				
			||||||
 | 
					                        &msg, 1, argParse->current_command->args[i]->long_opt);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                __catStr(&msg, 1, RESET " is required");
 | 
				
			||||||
 | 
					                argParseError(
 | 
				
			||||||
 | 
					                    argParse, argParse->current_command, msg, NULL); // 错误处理
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
@@ -751,12 +802,12 @@ char *argParseGenerateHelpForCommand(Command *command) {
 | 
				
			|||||||
    __catStr(&help_msg, 2, "\033[1;33mUsage\033[0m: ", command->name);
 | 
					    __catStr(&help_msg, 2, "\033[1;33mUsage\033[0m: ", command->name);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    switch (command->value_type) {
 | 
					    switch (command->value_type) {
 | 
				
			||||||
    case NOVALUE:
 | 
					    case ArgParseNOVALUE:
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
    case SINGLEVALUE:
 | 
					    case ArgParseSINGLEVALUE:
 | 
				
			||||||
        __catStr(&help_msg, 1, " <value>");
 | 
					        __catStr(&help_msg, 1, " <value>");
 | 
				
			||||||
        break;
 | 
					        break;
 | 
				
			||||||
    case MULTIVALUE:
 | 
					    case ArgParseMULTIVALUE:
 | 
				
			||||||
        __catStr(&help_msg, 1, " <value>...");
 | 
					        __catStr(&help_msg, 1, " <value>...");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -818,7 +869,7 @@ bool argParseCheckCommandTriggered(ArgParse *argParse, char *command_name) {
 | 
				
			|||||||
    return command->is_trigged;
 | 
					    return command->is_trigged;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
_Noreturn void argParseError(ArgParse   *argParse,
 | 
					NORETURN void argParseError(ArgParse   *argParse,
 | 
				
			||||||
                            Command    *lastCommand,
 | 
					                            Command    *lastCommand,
 | 
				
			||||||
                            const char *prefix,
 | 
					                            const char *prefix,
 | 
				
			||||||
                            const char *suffix) {
 | 
					                            const char *suffix) {
 | 
				
			||||||
@@ -841,7 +892,6 @@ _Noreturn void argParseError(ArgParse   *argParse,
 | 
				
			|||||||
            __catStr(&mgs, 2, "\n", suffix);
 | 
					            __catStr(&mgs, 2, "\n", suffix);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // printf("\033[1;31mERROR\033[0m: Last command is unknown\n");
 | 
					 | 
				
			||||||
        printf("%s\n", mgs);
 | 
					        printf("%s\n", mgs);
 | 
				
			||||||
        free(mgs);
 | 
					        free(mgs);
 | 
				
			||||||
        free(help);
 | 
					        free(help);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -179,7 +179,7 @@ argParseFindGlobalArgs(ArgParse *argParse, char *name, bool short_flag) {
 | 
				
			|||||||
 * @return 成功返回true,失败返回false
 | 
					 * @return 成功返回true,失败返回false
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
bool argParseSetArgVal(CommandArgs *args, char *val) {
 | 
					bool argParseSetArgVal(CommandArgs *args, char *val) {
 | 
				
			||||||
    if (args->value_type == MULTIVALUE) { // 多值
 | 
					    if (args->value_type == ArgParseMULTIVALUE) { // 多值
 | 
				
			||||||
        args->val = realloc(args->val, (args->val_len + 1) * sizeof(char *));
 | 
					        args->val = realloc(args->val, (args->val_len + 1) * sizeof(char *));
 | 
				
			||||||
        if (args->val == NULL) {
 | 
					        if (args->val == NULL) {
 | 
				
			||||||
            return false;
 | 
					            return false;
 | 
				
			||||||
@@ -190,7 +190,7 @@ bool argParseSetArgVal(CommandArgs *args, char *val) {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        args->val_len++;
 | 
					        args->val_len++;
 | 
				
			||||||
        return true;
 | 
					        return true;
 | 
				
			||||||
    } else if (args->value_type == SINGLEVALUE) { // 单值
 | 
					    } else if (args->value_type == ArgParseSINGLEVALUE) { // 单值
 | 
				
			||||||
        if (args->val != NULL) {
 | 
					        if (args->val != NULL) {
 | 
				
			||||||
            free(args->val);
 | 
					            free(args->val);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -205,7 +205,7 @@ bool argParseSetArgVal(CommandArgs *args, char *val) {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        args->val_len = 1;
 | 
					        args->val_len = 1;
 | 
				
			||||||
        return true;
 | 
					        return true;
 | 
				
			||||||
    } else if (args->value_type == NOVALUE) { // 无值
 | 
					    } else if (args->value_type == ArgParseNOVALUE) { // 无值
 | 
				
			||||||
        return true;
 | 
					        return true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -213,7 +213,7 @@ bool argParseSetArgVal(CommandArgs *args, char *val) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool argParseSetCommandVal(Command *command, char *val) {
 | 
					bool argParseSetCommandVal(Command *command, char *val) {
 | 
				
			||||||
    if (command->value_type == MULTIVALUE) { // 多值
 | 
					    if (command->value_type == ArgParseMULTIVALUE) { // 多值
 | 
				
			||||||
        command->val =
 | 
					        command->val =
 | 
				
			||||||
            realloc(command->val, (command->val_len + 1) * sizeof(char *));
 | 
					            realloc(command->val, (command->val_len + 1) * sizeof(char *));
 | 
				
			||||||
        if (command->val == NULL) {
 | 
					        if (command->val == NULL) {
 | 
				
			||||||
@@ -225,7 +225,7 @@ bool argParseSetCommandVal(Command *command, char *val) {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        command->val_len++;
 | 
					        command->val_len++;
 | 
				
			||||||
        return true;
 | 
					        return true;
 | 
				
			||||||
    } else if (command->value_type == SINGLEVALUE) { // 单值
 | 
					    } else if (command->value_type == ArgParseSINGLEVALUE) { // 单值
 | 
				
			||||||
        if (command->val != NULL) {
 | 
					        if (command->val != NULL) {
 | 
				
			||||||
            free(command->val);
 | 
					            free(command->val);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -240,7 +240,7 @@ bool argParseSetCommandVal(Command *command, char *val) {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        command->val_len = 1;
 | 
					        command->val_len = 1;
 | 
				
			||||||
        return true;
 | 
					        return true;
 | 
				
			||||||
    } else if (command->value_type == NOVALUE) { // 无值
 | 
					    } else if (command->value_type == ArgParseNOVALUE) { // 无值
 | 
				
			||||||
        return true;
 | 
					        return true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return false;
 | 
					    return false;
 | 
				
			||||||
@@ -254,7 +254,7 @@ bool argParseSetCommandVal(Command *command, char *val) {
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
bool argParseSetVal(ArgParse *argParse, char *val) {
 | 
					bool argParseSetVal(ArgParse *argParse, char *val) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (argParse->value_type == MULTIVALUE) { // 多值
 | 
					    if (argParse->value_type == ArgParseMULTIVALUE) { // 多值
 | 
				
			||||||
        argParse->val =
 | 
					        argParse->val =
 | 
				
			||||||
            realloc(argParse->val, (argParse->val_len + 1) * sizeof(char *));
 | 
					            realloc(argParse->val, (argParse->val_len + 1) * sizeof(char *));
 | 
				
			||||||
        if (argParse->val == NULL) {
 | 
					        if (argParse->val == NULL) {
 | 
				
			||||||
@@ -266,7 +266,7 @@ bool argParseSetVal(ArgParse *argParse, char *val) {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        argParse->val_len++;
 | 
					        argParse->val_len++;
 | 
				
			||||||
        return true;
 | 
					        return true;
 | 
				
			||||||
    } else if (argParse->value_type == SINGLEVALUE) { // 单值
 | 
					    } else if (argParse->value_type == ArgParseSINGLEVALUE) { // 单值
 | 
				
			||||||
        if (argParse->val != NULL) {
 | 
					        if (argParse->val != NULL) {
 | 
				
			||||||
            free(argParse->val);
 | 
					            free(argParse->val);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -281,7 +281,7 @@ bool argParseSetVal(ArgParse *argParse, char *val) {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
        argParse->val_len = 1;
 | 
					        argParse->val_len = 1;
 | 
				
			||||||
        return true;
 | 
					        return true;
 | 
				
			||||||
    } else if (argParse->value_type == NOVALUE) { // 无值
 | 
					    } else if (argParse->value_type == ArgParseNOVALUE) { // 无值
 | 
				
			||||||
        return true;
 | 
					        return true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    return false;
 | 
					    return false;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -37,3 +37,12 @@ set_tests_properties(${PROJECT_NAME}unknow_command_arg PROPERTIES WILL_FAIL TRUE
 | 
				
			|||||||
add_executable(${PROJECT_NAME}val test_val.c)
 | 
					add_executable(${PROJECT_NAME}val test_val.c)
 | 
				
			||||||
target_link_libraries(${PROJECT_NAME}val CArgParse)
 | 
					target_link_libraries(${PROJECT_NAME}val CArgParse)
 | 
				
			||||||
add_test(${PROJECT_NAME}val ${PROJECT_NAME}val file1.txt file2.txt file3.txt -v -q)
 | 
					add_test(${PROJECT_NAME}val ${PROJECT_NAME}val file1.txt file2.txt file3.txt -v -q)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# 必填参数测试
 | 
				
			||||||
 | 
					add_executable(${PROJECT_NAME}required test_required.c)
 | 
				
			||||||
 | 
					target_link_libraries(${PROJECT_NAME}required CArgParse)
 | 
				
			||||||
 | 
					add_test(${PROJECT_NAME}required_1 ${PROJECT_NAME}required -f)
 | 
				
			||||||
 | 
					add_test(${PROJECT_NAME}required_2 ${PROJECT_NAME}required)
 | 
				
			||||||
 | 
					set_tests_properties(${PROJECT_NAME}required_2 PROPERTIES WILL_FAIL TRUE)
 | 
				
			||||||
 | 
					add_test(${PROJECT_NAME}required_3 ${PROJECT_NAME}required -h)
 | 
				
			||||||
 | 
					add_test(${PROJECT_NAME}required_4 ${PROJECT_NAME}required -f install -h)
 | 
				
			||||||
@@ -4,7 +4,7 @@
 | 
				
			|||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ArgParse *initArgParse() {
 | 
					ArgParse *initArgParse() {
 | 
				
			||||||
    ArgParse *argparse    = argParseInit("测试程序",NOVALUE);
 | 
					    ArgParse *argparse    = argParseInit("测试程序", ArgParseNOVALUE);
 | 
				
			||||||
    Command  *command     = NULL;
 | 
					    Command  *command     = NULL;
 | 
				
			||||||
    Command  *sub_command = NULL;
 | 
					    Command  *sub_command = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -16,13 +16,24 @@ ArgParse *initArgParse() {
 | 
				
			|||||||
                         NULL,
 | 
					                         NULL,
 | 
				
			||||||
                         NULL,
 | 
					                         NULL,
 | 
				
			||||||
                         false,
 | 
					                         false,
 | 
				
			||||||
                         NOVALUE);
 | 
					                         ArgParseNOVALUE);
 | 
				
			||||||
    argParseAddGlobalArg(
 | 
					    argParseAddGlobalArg(argparse,
 | 
				
			||||||
        argparse, "-q", "--quiet", "Quiet mode", NULL, NULL, false, NOVALUE);
 | 
					                         "-q",
 | 
				
			||||||
 | 
					                         "--quiet",
 | 
				
			||||||
 | 
					                         "Quiet mode",
 | 
				
			||||||
 | 
					                         NULL,
 | 
				
			||||||
 | 
					                         NULL,
 | 
				
			||||||
 | 
					                         false,
 | 
				
			||||||
 | 
					                         ArgParseNOVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // add arguments
 | 
					    // add arguments
 | 
				
			||||||
    command = argParseAddCommand(
 | 
					    command = argParseAddCommand(argparse,
 | 
				
			||||||
        argparse, "install", "Install the package", NULL, NULL, NULL, NOVALUE);
 | 
					                                 "install",
 | 
				
			||||||
 | 
					                                 "Install the package",
 | 
				
			||||||
 | 
					                                 NULL,
 | 
				
			||||||
 | 
					                                 NULL,
 | 
				
			||||||
 | 
					                                 NULL,
 | 
				
			||||||
 | 
					                                 ArgParseNOVALUE);
 | 
				
			||||||
    argParseAddArg(command,
 | 
					    argParseAddArg(command,
 | 
				
			||||||
                   "-i",
 | 
					                   "-i",
 | 
				
			||||||
                   "--index",
 | 
					                   "--index",
 | 
				
			||||||
@@ -30,7 +41,7 @@ ArgParse *initArgParse() {
 | 
				
			|||||||
                   "https://example.com",
 | 
					                   "https://example.com",
 | 
				
			||||||
                   NULL,
 | 
					                   NULL,
 | 
				
			||||||
                   false,
 | 
					                   false,
 | 
				
			||||||
                   SINGLEVALUE);
 | 
					                   ArgParseSINGLEVALUE);
 | 
				
			||||||
    argParseAddArg(command,
 | 
					    argParseAddArg(command,
 | 
				
			||||||
                   "-f",
 | 
					                   "-f",
 | 
				
			||||||
                   "--file",
 | 
					                   "--file",
 | 
				
			||||||
@@ -38,7 +49,7 @@ ArgParse *initArgParse() {
 | 
				
			|||||||
                   "package.json",
 | 
					                   "package.json",
 | 
				
			||||||
                   NULL,
 | 
					                   NULL,
 | 
				
			||||||
                   false,
 | 
					                   false,
 | 
				
			||||||
                   MULTIVALUE);
 | 
					                   ArgParseMULTIVALUE);
 | 
				
			||||||
    argParseAddArg(command,
 | 
					    argParseAddArg(command,
 | 
				
			||||||
                   "-p",
 | 
					                   "-p",
 | 
				
			||||||
                   "--package",
 | 
					                   "--package",
 | 
				
			||||||
@@ -46,10 +57,15 @@ ArgParse *initArgParse() {
 | 
				
			|||||||
                   "package.json",
 | 
					                   "package.json",
 | 
				
			||||||
                   NULL,
 | 
					                   NULL,
 | 
				
			||||||
                   false,
 | 
					                   false,
 | 
				
			||||||
                   MULTIVALUE);
 | 
					                   ArgParseMULTIVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    sub_command = argParseAddSubCommand(
 | 
					    sub_command = argParseAddSubCommand(command,
 | 
				
			||||||
        command, "tools", "Install tools", NULL, NULL, NULL, MULTIVALUE);
 | 
					                                        "tools",
 | 
				
			||||||
 | 
					                                        "Install tools",
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        ArgParseMULTIVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    argParseAddArg(sub_command,
 | 
					    argParseAddArg(sub_command,
 | 
				
			||||||
                   "-t",
 | 
					                   "-t",
 | 
				
			||||||
@@ -58,9 +74,14 @@ ArgParse *initArgParse() {
 | 
				
			|||||||
                   "Tool name",
 | 
					                   "Tool name",
 | 
				
			||||||
                   NULL,
 | 
					                   NULL,
 | 
				
			||||||
                   true,
 | 
					                   true,
 | 
				
			||||||
                   MULTIVALUE);
 | 
					                   ArgParseMULTIVALUE);
 | 
				
			||||||
    sub_command = argParseAddSubCommand(
 | 
					    sub_command = argParseAddSubCommand(command,
 | 
				
			||||||
        command, "tools_sub", "Install tools", NULL, NULL, NULL, MULTIVALUE);
 | 
					                                        "tools_sub",
 | 
				
			||||||
 | 
					                                        "Install tools",
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        ArgParseMULTIVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    argParseAddArg(sub_command,
 | 
					    argParseAddArg(sub_command,
 | 
				
			||||||
                   "-s",
 | 
					                   "-s",
 | 
				
			||||||
@@ -69,7 +90,7 @@ ArgParse *initArgParse() {
 | 
				
			|||||||
                   "tools subcommand test",
 | 
					                   "tools subcommand test",
 | 
				
			||||||
                   NULL,
 | 
					                   NULL,
 | 
				
			||||||
                   true,
 | 
					                   true,
 | 
				
			||||||
                   MULTIVALUE);
 | 
					                   ArgParseMULTIVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    command = argParseAddCommand(argparse,
 | 
					    command = argParseAddCommand(argparse,
 | 
				
			||||||
                                 "uninstall",
 | 
					                                 "uninstall",
 | 
				
			||||||
@@ -77,7 +98,7 @@ ArgParse *initArgParse() {
 | 
				
			|||||||
                                 NULL,
 | 
					                                 NULL,
 | 
				
			||||||
                                 NULL,
 | 
					                                 NULL,
 | 
				
			||||||
                                 NULL,
 | 
					                                 NULL,
 | 
				
			||||||
                                 SINGLEVALUE);
 | 
					                                 ArgParseSINGLEVALUE);
 | 
				
			||||||
    argParseAddArg(command,
 | 
					    argParseAddArg(command,
 | 
				
			||||||
                   "-p",
 | 
					                   "-p",
 | 
				
			||||||
                   "--package",
 | 
					                   "--package",
 | 
				
			||||||
@@ -85,7 +106,7 @@ ArgParse *initArgParse() {
 | 
				
			|||||||
                   "Package name",
 | 
					                   "Package name",
 | 
				
			||||||
                   NULL,
 | 
					                   NULL,
 | 
				
			||||||
                   true,
 | 
					                   true,
 | 
				
			||||||
                   MULTIVALUE);
 | 
					                   ArgParseMULTIVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return argparse;
 | 
					    return argparse;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -10,7 +10,6 @@ int main(int argc, char *argv[]) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    const char *testv[3] = {"testpackge1", "testpackge2", "testpackge3"};
 | 
					    const char *testv[3] = {"testpackge1", "testpackge2", "testpackge3"};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
    // Test -p
 | 
					    // Test -p
 | 
				
			||||||
    char *val            = argParseGetCurArg(argparse, "-p");
 | 
					    char *val            = argParseGetCurArg(argparse, "-p");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -27,8 +26,6 @@ int main(int argc, char *argv[]) {
 | 
				
			|||||||
    printf("-i Value: %s\n", val_i);
 | 
					    printf("-i Value: %s\n", val_i);
 | 
				
			||||||
    assert(strcmp(val_i, "www.test.com") == 0);
 | 
					    assert(strcmp(val_i, "www.test.com") == 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // Test -f
 | 
					    // Test -f
 | 
				
			||||||
    len                  = 0;
 | 
					    len                  = 0;
 | 
				
			||||||
    const char *testf[2] = {"file1.txt", "file2.txt"};
 | 
					    const char *testf[2] = {"file1.txt", "file2.txt"};
 | 
				
			||||||
@@ -38,7 +35,6 @@ int main(int argc, char *argv[]) {
 | 
				
			|||||||
        assert(strcmp(val_f[i], testf[i]) == 0);
 | 
					        assert(strcmp(val_f[i], testf[i]) == 0);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
    argParseFree(argparse);
 | 
					    argParseFree(argparse);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										144
									
								
								tests/test_required.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										144
									
								
								tests/test_required.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,144 @@
 | 
				
			|||||||
 | 
					#include "ArgParse.h"
 | 
				
			||||||
 | 
					#include <assert.h>
 | 
				
			||||||
 | 
					#include <stdbool.h>
 | 
				
			||||||
 | 
					#include <stdio.h>
 | 
				
			||||||
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					ArgParse *initArgParse() {
 | 
				
			||||||
 | 
					    ArgParse *argparse    = argParseInit("测试程序", ArgParseNOVALUE);
 | 
				
			||||||
 | 
					    Command  *command     = NULL;
 | 
				
			||||||
 | 
					    Command  *sub_command = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // add global arguments
 | 
				
			||||||
 | 
					    argParseAddGlobalArg(argparse,
 | 
				
			||||||
 | 
					                         "-v",
 | 
				
			||||||
 | 
					                         "--version",
 | 
				
			||||||
 | 
					                         "Show version",
 | 
				
			||||||
 | 
					                         NULL,
 | 
				
			||||||
 | 
					                         NULL,
 | 
				
			||||||
 | 
					                         false,
 | 
				
			||||||
 | 
					                         ArgParseNOVALUE);
 | 
				
			||||||
 | 
					    argParseAddGlobalArg(argparse,
 | 
				
			||||||
 | 
					                         "-f",
 | 
				
			||||||
 | 
					                         "--file",
 | 
				
			||||||
 | 
					                         "File name",
 | 
				
			||||||
 | 
					                         NULL,
 | 
				
			||||||
 | 
					                         NULL,
 | 
				
			||||||
 | 
					                         true,
 | 
				
			||||||
 | 
					                         ArgParseNOVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // add arguments
 | 
				
			||||||
 | 
					    command = argParseAddCommand(argparse,
 | 
				
			||||||
 | 
					                                 "install",
 | 
				
			||||||
 | 
					                                 "Install the package",
 | 
				
			||||||
 | 
					                                 NULL,
 | 
				
			||||||
 | 
					                                 NULL,
 | 
				
			||||||
 | 
					                                 NULL,
 | 
				
			||||||
 | 
					                                 ArgParseNOVALUE);
 | 
				
			||||||
 | 
					    argParseAddArg(command,
 | 
				
			||||||
 | 
					                   "-i",
 | 
				
			||||||
 | 
					                   "--index",
 | 
				
			||||||
 | 
					                   "Index URL",
 | 
				
			||||||
 | 
					                   "https://example.com",
 | 
				
			||||||
 | 
					                   NULL,
 | 
				
			||||||
 | 
					                   false,
 | 
				
			||||||
 | 
					                   ArgParseSINGLEVALUE);
 | 
				
			||||||
 | 
					    argParseAddArg(command,
 | 
				
			||||||
 | 
					                   "-f",
 | 
				
			||||||
 | 
					                   "--file",
 | 
				
			||||||
 | 
					                   "Package file",
 | 
				
			||||||
 | 
					                   "package.json",
 | 
				
			||||||
 | 
					                   NULL,
 | 
				
			||||||
 | 
					                   true,
 | 
				
			||||||
 | 
					                   ArgParseMULTIVALUE);
 | 
				
			||||||
 | 
					    argParseAddArg(command,
 | 
				
			||||||
 | 
					                   "-p",
 | 
				
			||||||
 | 
					                   "--package",
 | 
				
			||||||
 | 
					                   "Package file",
 | 
				
			||||||
 | 
					                   "package.json",
 | 
				
			||||||
 | 
					                   NULL,
 | 
				
			||||||
 | 
					                   false,
 | 
				
			||||||
 | 
					                   ArgParseMULTIVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    sub_command = argParseAddSubCommand(command,
 | 
				
			||||||
 | 
					                                        "tools",
 | 
				
			||||||
 | 
					                                        "Install tools",
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        ArgParseMULTIVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    argParseAddArg(sub_command,
 | 
				
			||||||
 | 
					                   "-t",
 | 
				
			||||||
 | 
					                   "--tool",
 | 
				
			||||||
 | 
					                   "Tool name",
 | 
				
			||||||
 | 
					                   "Tool name",
 | 
				
			||||||
 | 
					                   NULL,
 | 
				
			||||||
 | 
					                   true,
 | 
				
			||||||
 | 
					                   ArgParseMULTIVALUE);
 | 
				
			||||||
 | 
					    sub_command = argParseAddSubCommand(command,
 | 
				
			||||||
 | 
					                                        "tools_sub",
 | 
				
			||||||
 | 
					                                        "Install tools",
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        ArgParseMULTIVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    argParseAddArg(sub_command,
 | 
				
			||||||
 | 
					                   "-s",
 | 
				
			||||||
 | 
					                   "--source",
 | 
				
			||||||
 | 
					                   "test_source",
 | 
				
			||||||
 | 
					                   "tools subcommand test",
 | 
				
			||||||
 | 
					                   NULL,
 | 
				
			||||||
 | 
					                   true,
 | 
				
			||||||
 | 
					                   ArgParseMULTIVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    command = argParseAddCommand(argparse,
 | 
				
			||||||
 | 
					                                 "uninstall",
 | 
				
			||||||
 | 
					                                 "Uninstall the package",
 | 
				
			||||||
 | 
					                                 NULL,
 | 
				
			||||||
 | 
					                                 NULL,
 | 
				
			||||||
 | 
					                                 NULL,
 | 
				
			||||||
 | 
					                                 ArgParseSINGLEVALUE);
 | 
				
			||||||
 | 
					    argParseAddArg(command,
 | 
				
			||||||
 | 
					                   "-p",
 | 
				
			||||||
 | 
					                   "--package",
 | 
				
			||||||
 | 
					                   "Package name",
 | 
				
			||||||
 | 
					                   "Package name",
 | 
				
			||||||
 | 
					                   NULL,
 | 
				
			||||||
 | 
					                   true,
 | 
				
			||||||
 | 
					                   ArgParseMULTIVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return argparse;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					int main(int argc, char *argv[]) {
 | 
				
			||||||
 | 
					    ArgParse *argparse = initArgParse();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    argParseParse(argparse, argc, argv);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    char *val = argParseGetVal(argparse);
 | 
				
			||||||
 | 
					    if (val) {
 | 
				
			||||||
 | 
					        printf("val: %s\n", val);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // int    len        = 0;
 | 
				
			||||||
 | 
					    // char **vals       = argParseGetValList(argparse, &len);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // char *test_val[3] = {"file1.txt", "file2.txt", "file3.txt"};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // if (vals) {
 | 
				
			||||||
 | 
					    //     for (int i = 0; i < len; i++) {
 | 
				
			||||||
 | 
					    //         printf("vals: %s\n", vals[i]);
 | 
				
			||||||
 | 
					    //         assert(strcmp(vals[i], test_val[i]) == 0);
 | 
				
			||||||
 | 
					    //     }
 | 
				
			||||||
 | 
					    // }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // assert(argParseCheckGlobalTriggered(argparse, "-v"));
 | 
				
			||||||
 | 
					    // assert(argParseCheckGlobalTriggered(argparse, "-f"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    argParseFree(argparse);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -8,12 +8,11 @@ int main(int argc, char *argv[]) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    argParseParse(argparse, argc, argv);
 | 
					    argParseParse(argparse, argc, argv);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    char *command_name = argParseGetCurCommandName(argparse);
 | 
					    char *command_name = argParseGetCurCommandName(argparse);
 | 
				
			||||||
    printf("command name: %s\n", command_name);
 | 
					    printf("command name: %s\n", command_name);
 | 
				
			||||||
    assert(strcmp(command_name, "tools") == 0);
 | 
					    assert(strcmp(command_name, "tools") == 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    char * val = argParseGetCurArg(argparse, "-t");
 | 
					    char *val = argParseGetCurArg(argparse, "-t");
 | 
				
			||||||
    printf("tools -t: %s\n", val);
 | 
					    printf("tools -t: %s\n", val);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    argParseFree(argparse);
 | 
					    argParseFree(argparse);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,7 +7,6 @@ int main(int argc, char *argv[]) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    argParseParse(argparse, argc, argv);
 | 
					    argParseParse(argparse, argc, argv);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 | 
				
			||||||
    argParseFree(argparse);
 | 
					    argParseFree(argparse);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,7 +5,7 @@
 | 
				
			|||||||
#include <string.h>
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ArgParse *initArgParse() {
 | 
					ArgParse *initArgParse() {
 | 
				
			||||||
    ArgParse *argparse    = argParseInit("测试程序", MULTIVALUE);
 | 
					    ArgParse *argparse    = argParseInit("测试程序", ArgParseMULTIVALUE);
 | 
				
			||||||
    Command  *command     = NULL;
 | 
					    Command  *command     = NULL;
 | 
				
			||||||
    Command  *sub_command = NULL;
 | 
					    Command  *sub_command = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -17,13 +17,24 @@ ArgParse *initArgParse() {
 | 
				
			|||||||
                         NULL,
 | 
					                         NULL,
 | 
				
			||||||
                         NULL,
 | 
					                         NULL,
 | 
				
			||||||
                         false,
 | 
					                         false,
 | 
				
			||||||
                         NOVALUE);
 | 
					                         ArgParseNOVALUE);
 | 
				
			||||||
    argParseAddGlobalArg(
 | 
					    argParseAddGlobalArg(argparse,
 | 
				
			||||||
        argparse, "-q", "--quiet", "Quiet mode", NULL, NULL, false, NOVALUE);
 | 
					                         "-q",
 | 
				
			||||||
 | 
					                         "--quiet",
 | 
				
			||||||
 | 
					                         "Quiet mode",
 | 
				
			||||||
 | 
					                         NULL,
 | 
				
			||||||
 | 
					                         NULL,
 | 
				
			||||||
 | 
					                         false,
 | 
				
			||||||
 | 
					                         ArgParseNOVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // add arguments
 | 
					    // add arguments
 | 
				
			||||||
    command = argParseAddCommand(
 | 
					    command = argParseAddCommand(argparse,
 | 
				
			||||||
        argparse, "install", "Install the package", NULL, NULL, NULL, NOVALUE);
 | 
					                                 "install",
 | 
				
			||||||
 | 
					                                 "Install the package",
 | 
				
			||||||
 | 
					                                 NULL,
 | 
				
			||||||
 | 
					                                 NULL,
 | 
				
			||||||
 | 
					                                 NULL,
 | 
				
			||||||
 | 
					                                 ArgParseNOVALUE);
 | 
				
			||||||
    argParseAddArg(command,
 | 
					    argParseAddArg(command,
 | 
				
			||||||
                   "-i",
 | 
					                   "-i",
 | 
				
			||||||
                   "--index",
 | 
					                   "--index",
 | 
				
			||||||
@@ -31,7 +42,7 @@ ArgParse *initArgParse() {
 | 
				
			|||||||
                   "https://example.com",
 | 
					                   "https://example.com",
 | 
				
			||||||
                   NULL,
 | 
					                   NULL,
 | 
				
			||||||
                   false,
 | 
					                   false,
 | 
				
			||||||
                   SINGLEVALUE);
 | 
					                   ArgParseSINGLEVALUE);
 | 
				
			||||||
    argParseAddArg(command,
 | 
					    argParseAddArg(command,
 | 
				
			||||||
                   "-f",
 | 
					                   "-f",
 | 
				
			||||||
                   "--file",
 | 
					                   "--file",
 | 
				
			||||||
@@ -39,7 +50,7 @@ ArgParse *initArgParse() {
 | 
				
			|||||||
                   "package.json",
 | 
					                   "package.json",
 | 
				
			||||||
                   NULL,
 | 
					                   NULL,
 | 
				
			||||||
                   false,
 | 
					                   false,
 | 
				
			||||||
                   MULTIVALUE);
 | 
					                   ArgParseMULTIVALUE);
 | 
				
			||||||
    argParseAddArg(command,
 | 
					    argParseAddArg(command,
 | 
				
			||||||
                   "-p",
 | 
					                   "-p",
 | 
				
			||||||
                   "--package",
 | 
					                   "--package",
 | 
				
			||||||
@@ -47,10 +58,15 @@ ArgParse *initArgParse() {
 | 
				
			|||||||
                   "package.json",
 | 
					                   "package.json",
 | 
				
			||||||
                   NULL,
 | 
					                   NULL,
 | 
				
			||||||
                   false,
 | 
					                   false,
 | 
				
			||||||
                   MULTIVALUE);
 | 
					                   ArgParseMULTIVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    sub_command = argParseAddSubCommand(
 | 
					    sub_command = argParseAddSubCommand(command,
 | 
				
			||||||
        command, "tools", "Install tools", NULL, NULL, NULL, MULTIVALUE);
 | 
					                                        "tools",
 | 
				
			||||||
 | 
					                                        "Install tools",
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        ArgParseMULTIVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    argParseAddArg(sub_command,
 | 
					    argParseAddArg(sub_command,
 | 
				
			||||||
                   "-t",
 | 
					                   "-t",
 | 
				
			||||||
@@ -59,9 +75,14 @@ ArgParse *initArgParse() {
 | 
				
			|||||||
                   "Tool name",
 | 
					                   "Tool name",
 | 
				
			||||||
                   NULL,
 | 
					                   NULL,
 | 
				
			||||||
                   true,
 | 
					                   true,
 | 
				
			||||||
                   MULTIVALUE);
 | 
					                   ArgParseMULTIVALUE);
 | 
				
			||||||
    sub_command = argParseAddSubCommand(
 | 
					    sub_command = argParseAddSubCommand(command,
 | 
				
			||||||
        command, "tools_sub", "Install tools", NULL, NULL, NULL, MULTIVALUE);
 | 
					                                        "tools_sub",
 | 
				
			||||||
 | 
					                                        "Install tools",
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        NULL,
 | 
				
			||||||
 | 
					                                        ArgParseMULTIVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    argParseAddArg(sub_command,
 | 
					    argParseAddArg(sub_command,
 | 
				
			||||||
                   "-s",
 | 
					                   "-s",
 | 
				
			||||||
@@ -70,7 +91,7 @@ ArgParse *initArgParse() {
 | 
				
			|||||||
                   "tools subcommand test",
 | 
					                   "tools subcommand test",
 | 
				
			||||||
                   NULL,
 | 
					                   NULL,
 | 
				
			||||||
                   true,
 | 
					                   true,
 | 
				
			||||||
                   MULTIVALUE);
 | 
					                   ArgParseMULTIVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    command = argParseAddCommand(argparse,
 | 
					    command = argParseAddCommand(argparse,
 | 
				
			||||||
                                 "uninstall",
 | 
					                                 "uninstall",
 | 
				
			||||||
@@ -78,7 +99,7 @@ ArgParse *initArgParse() {
 | 
				
			|||||||
                                 NULL,
 | 
					                                 NULL,
 | 
				
			||||||
                                 NULL,
 | 
					                                 NULL,
 | 
				
			||||||
                                 NULL,
 | 
					                                 NULL,
 | 
				
			||||||
                                 SINGLEVALUE);
 | 
					                                 ArgParseSINGLEVALUE);
 | 
				
			||||||
    argParseAddArg(command,
 | 
					    argParseAddArg(command,
 | 
				
			||||||
                   "-p",
 | 
					                   "-p",
 | 
				
			||||||
                   "--package",
 | 
					                   "--package",
 | 
				
			||||||
@@ -86,7 +107,7 @@ ArgParse *initArgParse() {
 | 
				
			|||||||
                   "Package name",
 | 
					                   "Package name",
 | 
				
			||||||
                   NULL,
 | 
					                   NULL,
 | 
				
			||||||
                   true,
 | 
					                   true,
 | 
				
			||||||
                   MULTIVALUE);
 | 
					                   ArgParseMULTIVALUE);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return argparse;
 | 
					    return argparse;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user