novo cabeçalho: args-parser

This commit is contained in:
Blau Araujo 2025-06-20 09:34:09 -03:00
parent 5914591860
commit 598b41f62d
3 changed files with 39 additions and 30 deletions

20
src/args-parser.c Normal file
View file

@ -0,0 +1,20 @@
#include <string.h>
#include "args-parser.h"
int parse_arg(const char *arg) {
// Argumento iniciado com '-'...
if (arg[0] == '-') {
if (strcmp(arg, "-a") == 0) {
return OPT_APPEND;
} else if (strcmp(arg, "-o") == 0) {
return OPT_OVERWRITE;
} else if (strcmp(arg, "-h") == 0) {
return OPT_HELP;
} else {
// Argumento inválido!
return OPT_ERROR;
}
}
// Sem traço é nome de arquivo...
return OPT_NAME;
}

17
src/args-parser.h Normal file
View file

@ -0,0 +1,17 @@
/*
* Strings definidas em messages.c
*/
#ifndef ARGS_PARSER_H
#define ARGS_PARSER_H
typedef enum {
OPT_ERROR = -1,
OPT_NAME,
OPT_OVERWRITE,
OPT_APPEND,
OPT_HELP
} Option;
int parse_arg(const char *arg);
#endif // ARGS_PARSER_H

View file

@ -1,22 +1,12 @@
/*
* Compilar com: gcc -Wall hdoc.c messages.c -o hdoc
* Compilar com: gcc -Wall hdoc.c messages.c args-parser.c -o hdoc
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "messages.h"
typedef enum {
OPT_ERROR = -1,
OPT_NAME,
OPT_OVERWRITE,
OPT_APPEND,
OPT_HELP
} Option;
int parse_arg(char *arg);
#include "args-parser.h"
int main(int argc, char **argv) {
char *file = NULL;
@ -77,21 +67,3 @@ int main(int argc, char **argv) {
return EXIT_SUCCESS;
}
int parse_arg(char *arg) {
// Argumento iniciado com '-'...
if (arg[0] == '-') {
if (strcmp(arg, "-a") == 0) {
return OPT_APPEND;
} else if (strcmp(arg, "-o") == 0) {
return OPT_OVERWRITE;
} else if (strcmp(arg, "-h") == 0) {
return OPT_HELP;
} else {
// Argumento inválido!
return OPT_ERROR;
}
}
// Sem traço é nome de arquivo...
return OPT_NAME;
}