C 명령줄 인수 구문 분석
C에서 두 파일을 한 줄씩, 한 단어씩, 또는 한 글자씩 비교할 수 있는 프로그램을 작성하려고 합니다. 옵션에서 수 합니다.-l,-w,-i ★★★★★★★★★★★★★★★★★」--
- (가) 일
-l는 파일을 한 줄씩 비교합니다. - (가) 일
-w한 한 글자 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , - (가) 일
--다음 인수가 첫 번째 파일명으로 자동 간주됩니다. - (가) 일
-i대소문자를 구분하지 않는 방법으로 비교합니다. - 는 기본적으로 파일을 문자별로 비교합니다.
-w ★★★★★★★★★★★★★★★★★」-l동시에 입력되지 않고 파일이 2개 이상일 수 없습니다.
명령줄 인수를 구문 분석하는 것부터 시작해야 할 지 모르겠습니다.
이게 내가 모든 걸 위해 생각해낸 코드야아직 에러 체크를 하지 않았는데, 너무 복잡하게 쓰고 있는 건가요?
/*
* Functions to compare files.
*/
int compare_line();
int compare_word();
int compare_char();
int case_insens();
/*
* Program to compare the information in two files and print message saying
* whether or not this was successful.
*/
int main(int argc, char* argv[])
{
/* Loop counter */
size_t i = 0;
/* Variables for functions */
int caseIns = 0;
int line = 0;
int word = 0;
/* File pointers */
FILE *fp1, *fp2;
/*
* Read through command-line arguments for options.
*/
for (i = 1; i < argc; i++)
{
printf("argv[%u] = %s\n", i, argv[i]);
if (argv[i][0] == '-')
{
if (argv[i][1] == 'i')
{
caseIns = 1;
}
if (argv[i][1] == 'l')
{
line = 1;
}
if (argv[i][1] == 'w')
{
word = 1;
}
if (argv[i][1] == '-')
{
fp1 = argv[i][2];
fp2 = argv[i][3];
}
else
{
printf("Invalid option.");
return 2;
}
}
else
{
fp1(argv[i]);
fp2(argv[i][1]);
}
}
/*
* Check that files can be opened.
*/
if(((fp1 = fopen(fp1, "rb")) == NULL) || ((fp2 = fopen(fp2, "rb")) == NULL))
{
perror("fopen()");
return 3;
}
else
{
if (caseIns == 1)
{
if(line == 1 && word == 1)
{
printf("That is invalid.");
return 2;
}
if(line == 1 && word == 0)
{
if(compare_line(case_insens(fp1, fp2)) == 0)
return 0;
}
if(line == 0 && word == 1)
{
if(compare_word(case_insens(fp1, fp2)) == 0)
return 0;
}
else
{
if(compare_char(case_insens(fp1,fp2)) == 0)
return 0;
}
}
else
{
if(line == 1 && word == 1)
{
printf("That is invalid.");
return 2;
}
if(line == 1 && word == 0)
{
if(compare_line(fp1, fp2) == 0)
return 0;
}
if(line == 0 && word == 1)
{
if(compare_word(fp1, fp2) == 0)
return 0;
}
else
{
if(compare_char(fp1, fp2) == 0)
return 0;
}
}
}
return 1;
if(((fp1 = fclose(fp1)) == NULL) || (((fp2 = fclose(fp2)) == NULL)))
{
perror("fclose()");
return 3;
}
else
{
fp1 = fclose(fp1);
fp2 = fclose(fp2);
}
}
/*
* Function to compare two files line-by-line.
*/
int compare_line(FILE *fp1, FILE *fp2)
{
/* Buffer variables to store the lines in the file */
char buff1 [LINESIZE];
char buff2 [LINESIZE];
/* Check that neither is the end of file */
while((!feof(fp1)) && (!feof(fp2)))
{
/* Go through files line by line */
fgets(buff1, LINESIZE, fp1);
fgets(buff2, LINESIZE, fp2);
}
/* Compare files line by line */
if(strcmp(buff1, buff2) == 0)
{
printf("Files are equal.\n");
return 0;
}
printf("Files are not equal.\n");
return 1;
}
/*
* Function to compare two files word-by-word.
*/
int compare_word(FILE *fp1, FILE *fp2)
{
/* File pointers */
FILE *fp1, *fp2;
/* Arrays to store words */
char fp1words[LINESIZE];
char fp2words[LINESIZE];
if(strtok(fp1, " ") == NULL || strtok(fp2, " ") == NULL)
{
printf("File is empty. Cannot compare.\n");
return 0;
}
else
{
fp1words = strtok(fp1, " ");
fp2words = strtok(fp2, " ");
if(fp1words == fp2words)
{
fputs(fp1words);
fputs(fp2words);
printf("Files are equal.\n");
return 0;
}
}
return 1;
}
/*
* Function to compare two files character by character.
*/
int compare_char(FILE *fp1,FILE *fp2)
{
/* Variables to store the characters from both files */
int c;
int d;
/* Buffer variables to store chars */
char buff1 [LINESIZE];
char buff2 [LINESIZE];
while(((c = fgetc(fp1))!= EOF) && (((d = fgetc(fp2))!=EOF)))
{
if(c == d)
{
if((fscanf(fp1, "%c", buff1)) == (fscanf(fp2, "%c", buff2)))
{
printf("Files have equivalent characters.\n");
return 1;
break;
}
}
}
return 0;
}
/*
* Function to compare two files in a case-insensitive manner.
*/
int case_insens(FILE *fp1, FILE *fp2, size_t n)
{
/* Pointers for files. */
FILE *fp1, *fp2;
/* Variable to go through files. */
size_t i = 0;
/* Arrays to store file information. */
char fp1store[LINESIZE];
char fp2store[LINESIZE];
while(!feof(fp1) && !feof(fp2))
{
for(i = 0; i < n; i++)
{
fscanf(fp1, "%s", fp1store);
fscanf(fp2, "%s", fp2store);
fp1store = tolower(fp1store);
fp2store = tolower(fp2store);
return 1;
}
}
return 0;
}
내가 알기로는 C에서 명령줄 인수를 해석하는 가장 일반적인 방법은 다음 3가지입니다.
- 겟옵트)
#include <unistd.h>POSIX C 라이브러리)에서 간단한 인수 해석 작업을 해결할 수 있습니다.bash에 대해 잘 알고 계시다면 bash의 getopt 빌트인은 GNU libc의 Getopt를 기반으로 합니다. - Argp (
#include <argp.h>GNU C Library)를 통해 보다 복잡한 작업을 해결할 수 있으며 다음과 같은 작업을 처리할 수 있습니다.-?,--help도움말 메시지(이메일 주소 포함)-V,--version버전 정보--usage용도의 메시지
- 직접 하는 것은 권장하지 않습니다.잘못되거나 품질이 저하될 가능성이 너무 많기 때문에 다른 사람에게 제공하는 프로그램에는 권장하지 않습니다.옵션 해석을 중지하기 위해 '--'를 잊어버리는 일반적인 실수는 하나의 예에 불과합니다.
GNU C 라이브러리 문서에는 Getopt 및 Argp에 대한 몇 가지 좋은 예가 있습니다.
- http://www.gnu.org/software/libc/manual/html_node/Getopt.html
- http://www.gnu.org/software/libc/manual/html_node/Argp.html
Getopt 사용 예시
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
bool isCaseInsensitive = false;
int opt;
enum { CHARACTER_MODE, WORD_MODE, LINE_MODE } mode = CHARACTER_MODE;
while ((opt = getopt(argc, argv, "ilw")) != -1) {
switch (opt) {
case 'i': isCaseInsensitive = true; break;
case 'l': mode = LINE_MODE; break;
case 'w': mode = WORD_MODE; break;
default:
fprintf(stderr, "Usage: %s [-ilw] [file...]\n", argv[0]);
exit(EXIT_FAILURE);
}
}
// Now optind (declared extern int by <unistd.h>) is the index of the first non-option argument.
// If it is >= argc, there were no non-option arguments.
// ...
}
Argp 사용 예시
#include <argp.h>
#include <stdbool.h>
const char *argp_program_version = "programname programversion";
const char *argp_program_bug_address = "<your@email.address>";
static char doc[] = "Your program description.";
static char args_doc[] = "[FILENAME]...";
static struct argp_option options[] = {
{ "line", 'l', 0, 0, "Compare lines instead of characters."},
{ "word", 'w', 0, 0, "Compare words instead of characters."},
{ "nocase", 'i', 0, 0, "Compare case insensitive instead of case sensitive."},
{ 0 }
};
struct arguments {
enum { CHARACTER_MODE, WORD_MODE, LINE_MODE } mode;
bool isCaseInsensitive;
};
static error_t parse_opt(int key, char *arg, struct argp_state *state) {
struct arguments *arguments = state->input;
switch (key) {
case 'l': arguments->mode = LINE_MODE; break;
case 'w': arguments->mode = WORD_MODE; break;
case 'i': arguments->isCaseInsensitive = true; break;
case ARGP_KEY_ARG: return 0;
default: return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp = { options, parse_opt, args_doc, doc, 0, 0, 0 };
int main(int argc, char *argv[])
{
struct arguments arguments;
arguments.mode = CHARACTER_MODE;
arguments.isCaseInsensitive = false;
argp_parse(&argp, argc, argv, 0, 0, &arguments);
// ...
}
직접 실행하는 예
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
bool isCaseInsensitive = false;
enum { CHARACTER_MODE, WORD_MODE, LINE_MODE } mode = CHARACTER_MODE;
size_t optind;
for (optind = 1; optind < argc && argv[optind][0] == '-'; optind++) {
switch (argv[optind][1]) {
case 'i': isCaseInsensitive = true; break;
case 'l': mode = LINE_MODE; break;
case 'w': mode = WORD_MODE; break;
default:
fprintf(stderr, "Usage: %s [-ilw] [file...]\n", argv[0]);
exit(EXIT_FAILURE);
}
}
argv += optind;
// *argv points to the remaining non-option arguments.
// If *argv is NULL, there were no non-option arguments.
// ...
}
면책사항:저는 Argp에 익숙하지 않습니다.이 예에는 오류가 포함되어 있을 수 있습니다.
Gengetopt는 매우 유용하다는 것을 알게 되었습니다.심플한 설정 파일로 원하는 옵션을 지정하면 어플리케이션과 링크할 수 있는 .c/.h 쌍이 생성됩니다.생성된 코드는 getopt_long을 사용하며, 가장 일반적인 종류의 명령줄 파라미터를 처리하는 것으로 보이며, 많은 시간을 절약할 수 있습니다.
gengetopt 입력 파일은 다음과 같습니다.
version "0.1"
package "myApp"
purpose "Does something useful."
# Options
option "filename" f "Input filename" string required
option "verbose" v "Increase program verbosity" flag off
option "id" i "Data ID" int required
option "value" r "Data value" multiple(1-) int optional
코드 생성은 간단하며 내뱉기cmdline.h ★★★★★★★★★★★★★★★★★」cmdline.c:
$ gengetopt --input=myApp.cmdline --include-getopt
생성된 코드는 쉽게 통합됩니다.
#include <stdio.h>
#include "cmdline.h"
int main(int argc, char ** argv) {
struct gengetopt_args_info ai;
if (cmdline_parser(argc, argv, &ai) != 0) {
exit(1);
}
printf("ai.filename_arg: %s\n", ai.filename_arg);
printf("ai.verbose_flag: %d\n", ai.verbose_flag);
printf("ai.id_arg: %d\n", ai.id_arg);
int i;
for (i = 0; i < ai.value_given; ++i) {
printf("ai.value_arg[%d]: %d\n", i, ai.value_arg[i]);
}
}
(배타적인지 등) 에 되어 있는 할 수 .gengetopt_args_info★★★★★★★★★★★★★★★★★★.
또는 을 사용합니다.
int iflag = 0;
enum { WORD_MODE, LINE_MODE } op_mode = WORD_MODE; // Default set
int opt;
while ((opt = getopt(argc, argv, "ilw") != -1)
{
switch (opt)
{
case 'i':
iflag = 1;
break;
case 'l':
op_mode = LINE_MODE;
break;
case 'w':
op_mode = WORD_MODE;
break;
default:
fprintf(stderr, "Usage: %s [-ilw] [file ...]\n", argv[0]);
exit(EXIT_FAILURE);
}
}
/* Process file names or stdin */
if (optind >= argc)
process(stdin, "(standard input)", op_mode);
else
{
int i;
for (i = optind; i < argc; i++)
{
FILE *fp = fopen(argv[i], "r");
if (fp == 0)
fprintf(stderr, "%s: failed to open %s (%d %s)\n",
argv[0], argv[i], errno, strerror(errno));
else
{
process(fp, argv[i], op_mode);
fclose(fp);
}
}
}
를 포함할 ), 쓸 를 결정해야 .op_mode합니다.process()- 아래 열거형에는 접근할 수 없습니다.은 함수 . 예를 들어, 열거형은 함수 밖으로 이동하는 것이 .op_mode링크가 파일 예: "예: "예: "예: "예상치 않은 static함수에 전달하지 않도록 합니다.에서는 처리하지 .-표준 입력의 동의어로서 독자를 위한 또 다른 연습입니다.:getopt()으로 --옵션을 종료할 수 있습니다.
컴파일러를 통해 위의 타이핑 버전을 실행한 적이 없습니다.오류가 있을 수 있습니다.
추가 학점을 받으려면 (라이브러리) 함수를 작성하십시오.
int filter(int argc, char **argv, int idx, int (*function)(FILE *fp, const char *fn));
합니다.getopt()입니다. ''는 '루프'를 합니다.-표준 입력으로 사용합니다.하면, 「 」의 「 」를 것에 해 주세요.op_mode정적 파일 범위 변수여야 합니다.filter()는 ""를 받습니다.argc,argv,optind및 처리 기능에 대한 포인터입니다.(EX)이 됩니다.IT_SUCCESS)는 모든 파일을 열 수 있었고 함수의 모든 호출이 0으로 보고되었으며, 그렇지 않으면 1(또는 EX)로 보고되었습니다.IT_FAILURE).이러한 기능을 사용하면 명령줄 또는 표준 입력에 지정된 파일을 읽는 Unix 스타일의 '필터' 프로그램을 쉽게 작성할 수 있습니다.
Docopt에는 C의 실장이 있어 매우 좋다고 생각했습니다.
명령줄 옵션을 설명하는 man-page의 표준화된 형식에서 docopt는 인수 파서를 추론하여 작성합니다.이것은 Python에서 시작되었습니다.Python 버전은 문자 그대로 docstring을 해석하고 dict를 반환합니다.이것을 C에서 하기 위해서는 조금 더 많은 작업이 필요하지만 사용하기에는 깨끗하고 외부 의존관계도 없습니다.
제 경적을 울리기 위해 제가 작성한 옵션 구문 분석 라이브러리(드롭)도 보시기 바랍니다.
- C 라이브러리(필요한 경우 C++ 래퍼 포함)입니다.
- 가볍습니다.
- 확장성이 있습니다(커스텀 인수 유형은 쉽게 추가할 수 있으며 기본 제공 인수 유형과 동등한 기반을 갖습니다).
- C 표준 라이브러리 이외에는 종속성이 없고 매우 휴대성이 뛰어나야 합니다(표준 C에 기재되어 있습니다).
- 매우 제한 없는 라이선스(zlib/libpng)를 가지고 있습니다.
많은 경우 제공하지 않는 기능 중 하나는 이전 옵션을 재정의하는 기능입니다.예를 들어 셸 에일리어스가 있는 경우:
alias bar="foo --flag1 --flag2 --flag3"
'이렇게 하다'를 합니다.bar, 하하와 함께--flag1비활성화 시 다음 작업을 수행할 수 있습니다.
bar --flag1=0
James Thiler의 "opt" 패키지를 사용할 수 있습니다.
다른 접근법보다 훨씬 단순한 예를 들어 다음과 같은 글을 올리겠습니다.
뛰어난 범용 C 라이브러리 libUCW가 있습니다.이 라이브러리에는 깔끔한 명령줄 옵션 해석과 컨피규레이션파일 로딩이 포함됩니다.
라이브러리에는 좋은 문서도 함께 제공되며 다른 유용한 정보(고속 I/O, 데이터 구조, 할당자 등)도 포함되어 있지만 별도로 사용할 수 있습니다.
lib 예시UCW 옵션파서(라이브러리 문서에서)
#include <ucw/lib.h>
#include <ucw/opt.h>
int english;
int sugar;
int verbose;
char *tea_name;
static struct opt_section options = {
OPT_ITEMS {
OPT_HELP("A simple tea boiling console."),
OPT_HELP("Usage: teapot [options] name-of-the-tea"),
OPT_HELP(""),
OPT_HELP("Options:"),
OPT_HELP_OPTION,
OPT_BOOL('e', "english-style", english, 0, "\tEnglish style (with milk)"),
OPT_INT('s', "sugar", sugar, OPT_REQUIRED_VALUE, "<spoons>\tAmount of sugar (in teaspoons)"),
OPT_INC('v', "verbose", verbose, 0, "\tVerbose (the more -v, the more verbose)"),
OPT_STRING(OPT_POSITIONAL(1), NULL, tea_name, OPT_REQUIRED, ""),
OPT_END
}
};
int main(int argc, char **argv)
{
opt_parse(&options, argv+1);
return 0;
}
저는 몇 가지 문제가 있었던 POpt와 유사한 주장을 해석하는 작은 라이브러리 XOpt를 썼습니다.GNU 스타일의 인수 해석을 사용하며 POpt와 매우 유사한 인터페이스를 가지고 있습니다.
가끔 사용하면서 큰 성공을 거두고 있습니다.어디서나 쓸 수 있어요
#include <stdio.h>
int main(int argc, char **argv)
{
size_t i;
size_t filename_i = -1;
for (i = 0; i < argc; i++)
{
char const *option = argv[i];
if (option[0] == '-')
{
printf("I am a flagged option");
switch (option[1])
{
case 'a':
/*someting*/
break;
case 'b':
break;
case '-':
/* "--" -- the next argument will be a file.*/
filename_i = i;
i = i + 1;
break;
default:
printf("flag not recognised %s", option);
break;
}
}
else
{
printf("I am a positional argument");
}
/* At this point, if -- was specified, then filename_i contains the index
into argv that contains the filename. If -- was not specified, then filename_i will be -1*/
}
return 0;
}
C의 명령줄 인수를 해석하기 위한 명령어템플릿
C:> programName -w -- fileOne 。txt fileTwo.txt
BOOL argLine = FALSE;
BOOL argWord = FALSE;
BOOL argChar = FALSE;
char * fileName1 = NULL;
char * fileName2 = NULL;
int main(int argc, char * argv[]) {
int i;
printf("Argument count=%d\n",argc);
for (i = 0; i < argc; i++) {
printf("Argument %s\n",argv[i]);
if (strcmp(argv[i],"-l")==0) {
argLine = TRUE;
printf(" argLine=TRUE\n");
}
else if (strcmp(argv[i],"-w")==0) {
argWord = TRUE;
printf(" argWord=TRUE\n");
}
else if (strcmp(argv[i],"-c")==0) {
argChar = TRUE;
printf(" argChar=TRUE\n");
}
else if (strcmp(argv[i],"--")==0) {
if (i+1 <= argc) {
fileName1 = argv[++i];
printf(" fileName1=%s\n",fileName1);
}
if (i+1 <= argc) {
fileName2 = argv[++i];
printf(" fileName2=%s\n",fileName2);
}
}
}
return 0;
}
/*
Here's a rough one not relying on any libraries.
Example:
-wi | -iw //word case insensitive
-li | -il //line case insensitive
-- file //specify the first filename (you could just get the files
as positional arguments in the else statement instead)
PS: don't mind the #define's, they're just pasting code :D
*/
#ifndef OPT_H
#define OPT_H
//specify option requires argument
#define require \
optarg = opt_pointer + 1; \
if (*optarg == '\0') \
{ \
if (++optind == argc) \
goto opt_err_arg; \
else \
optarg = argv[optind]; \
} \
opt_pointer = opt_null_terminator;
//start processing argv
#define opt \
int optind = 1; \
char *opt_pointer = argv[1]; \
char *optarg = NULL; \
char opt_null_terminator[2] = {'\0','\0'}; \
if (0) \
{ \
opt_err_arg: \
fprintf(stderr,"option %c requires argument.\n",*opt_pointer); \
return 1; \
opt_err_opt: \
fprintf(stderr,"option %c is invalid.\n",*opt_pointer); \
return 1; \
} \
for (; optind < argc; opt_pointer = argv[++optind]) \
if (*opt_pointer++ == '-') \
{ \
for (;;++opt_pointer) \
switch (*opt_pointer) \
{
//stop processing argv
#define done \
default: \
if (*opt_pointer != '\0') \
goto opt_err_opt; \
else \
goto opt_next; \
break; \
} \
opt_next:; \
}
#endif //opt.h
#include <stdio.h>
#include "opt.h"
int
main (int argc, char **argv)
{
#define by_character 0
#define by_word 1
#define by_line 2
int cmp = by_character;
int case_insensitive = 0;
opt
case 'h':
puts ("HELP!");
break;
case 'v':
puts ("fileCMP Version 1.0");
break;
case 'i':
case_insensitive = 1;
break;
case 'w':
cmp = by_word;
break;
case 'l':
cmp = by_line;
break;
case '-':required
printf("first filename: %s\n", optarg);
break;
done
else printf ("Positional Argument %s\n", argv[optind]);
return 0;
}
좋아요, 그게 긴 이야기의 시작입니다. C의 명령줄을 해석하는 짧은 중단입니다.
/**
* Helper function to parse the command line
* @param argc Argument Counter
* @param argv Argument Vector
* @param prog Program Instance Reference to fill with options
*/
bool parseCommandLine(int argc, char* argv[], DuplicateFileHardLinker* prog) {
bool pathAdded = false;
// Iterate over all arguments...
for (int i = 1; i<argc; i++) {
// Is argv a command line option?
if (argv[i][0] == '-' || argv[i][0] == '/') {
// ~~~~~~ Optionally Cut that part vvvvvvvvvvvvv for sake of simplicity ~~~~~~~
// Check for longer options
if (stricmp( &argv[i][1], "NoFileName") == 0 ||
strcmp( &argv[i][1], "q1" ) == 0 ) {
boNoFileNameLog = true;
} else if (strcmp( &argv[i][1], "HowAreYou?") == 0 ) {
logInfo( "SECRET FOUND: Well - wow I'm glad ya ask me.");
} else {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Now here comes the main thing:
//
// Check for one-character options
while (char option = *++argv[i]) {
switch (option) {
case '?':
// Show program usage
logInfo(L"Options:");
logInfo(L" /q\t>Quite mode");
logInfo(L" /v\t>Verbose mode");
logInfo(L" /d\t>Debug mode");
return false;
// Log options
case 'q':
setLogLevel(LOG_ERROR);
break;
case 'v':
setLogLevel(LOG_VERBOSE);
break;
case 'd':
setLogLevel(LOG_DEBUG);
break;
default:
logError(L"'%s' is an illegal command line option!"
" Use /? to see valid options!", option);
return false;
} // switch one-char-option
} // while one-char-options
} // else one vs longer options
} // if isArgAnOption
//
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^ So that's it! ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// What follows now is are some useful extras...
//
else {
// The command line options seems to be a path...
WCHAR tmpPath[MAX_PATH_LENGTH];
mbstowcs(tmpPath, argv[i], sizeof(tmpPath));
// Check if the path is existing!
//...
prog->addPath(tmpPath); // Comment or remove to get a working example
pathAdded = true;
}
}
// Check for parameters
if (!pathAdded) {
logError("You need to specify at least one folder to process!\n"
"Use /? to see valid options!");
return false;
}
return true;
}
int main(int argc, char* argv[]) {
try {
// Parse the command line
if ( !parseCommandLine(argc, argv, prog) ) {
return 1;
}
// I know that sample is just to show how the nicely parse command-line arguments
// So Please excuse more nice useful C-glatter that follows now...
}
catch ( LPCWSTR err ) {
DWORD dwError = GetLastError();
if ( wcslen(err) > 0 ) {
if ( dwError != 0 ) {
logError(dwError, err);
}
else {
logError(err);
}
}
return 2;
}
}
#define LOG_ERROR 1
#define LOG_INFO 0
#define LOG_VERBOSE -1
#define LOG_DEBUG -2
/** Logging level for the console output */
int logLevel = LOG_INFO;
void logError(LPCWSTR message, ...) {
va_list argp;
fwprintf(stderr, L"ERROR: ");
va_start(argp, message);
vfwprintf(stderr, message, argp);
va_end(argp);
fwprintf(stderr, L"\n");
}
void logInfo(LPCWSTR message, ...) {
if ( logLevel <= LOG_INFO ) {
va_list argp;
va_start(argp, message);
vwprintf(message, argp);
va_end(argp);
wprintf(L"\n");
}
}
이 버전에서는 다음 인수의 조합도 지원된다는 점에 주의해 주십시오.따라서 /h /s -> /hs 라고 하는 대신에, /hs 라고도 사용할 수 있습니다.
n번째 투고가 되어 죄송합니다만, 여기서 본 모든 스탠드아론 버전에는 그다지 만족하지 못했습니다.음, 도서관은 꽤 괜찮네요.그래서 저는 집에서 만든 것보다 libUCW 옵션 파서, Arg 또는 Getopt를 선호합니다.
변경할 수 있습니다.
*++argv[i]->(++argv*)[0]
더 길고 덜 신비롭지만 여전히 신비롭다.
좋아, 분해:자.
Argv[나는]->argv-char 이동 분야에서 접근i-th 요소이다.
++*...->는 argv-module을 1글자씩 전송합니다.
... [ 0 ] - > 는, 포인터에 따라서 문자를 읽습니다.
++(...) -> 괄호가 있기 때문에 포인터를 늘립니다.문자 값 자체는 증가시키지 않습니다.
C#에서 포인터가 '죽었다'는 건 너무 좋아요. 포인터 만세!!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int is_arg(int ac, char **argv, char *arg) {
if (ac < 2) {
return 0;
}
for(int x=1; x < ac; x++) {
if (0 == strcmp(argv[x], arg)) {
return x; // return position of arg
}
}
return 0; // arg not present
}
int main (int argc, char *argv[]) {
int z = 0;
if (argc < 2) {
printf("no args present, aborting.\n");
exit(1);
}
(z=is_arg(argc, argv, "bar")) ? printf("TRUE %d\n", z) : printf("FALSE\n");
(z=is_arg(argc, argv, "one bar")) ? printf("TRUE %d\n", z) : printf("FALSE\n");
(z=is_arg(argc, argv, "foo")) ? printf("TRUE %d\n", z) : printf("FALSE\n");
/* testing:
run: ./getopt two bar "one bar" foo
TRUE 2
TRUE 3
TRUE 4
run: ./getopt two bar one bar foo
TRUE 2
FALSE
TRUE 5
*/
return 0;
}
뻔뻔한 플러그.저는 clip이라는 또 다른 명령줄 파서의 저자입니다.파서는 크로스 플랫폼이며 다양한 컴파일러와 함께 작동합니다.GCC, clang, Turbo C, Open Watcom, 그리고 Microsoft Visual C++에서도 사용해 보았습니다.
언급URL : https://stackoverflow.com/questions/9642732/parsing-command-line-arguments-in-c
'programing' 카테고리의 다른 글
| 루프 내부 또는 외부 변수 선언 (0) | 2022.07.28 |
|---|---|
| 재귀 구성 요소에서 "프로포트를 직접 변환하지 않도록 하는 방법 (0) | 2022.07.28 |
| Enter 키를 누르기를 기다리지 않고 표준 입력에서 문자 캡처 (0) | 2022.07.28 |
| C에서 어레이를 참조로 전달하시겠습니까? (0) | 2022.07.28 |
| Java 8에서 LocalDateTime에서 밀리초를 얻는 방법 (0) | 2022.07.28 |