parallax 1.0
command-line based task/todo manager
Loading...
Searching...
No Matches
argparse.h File Reference

Contains functions for extracting content from the argument vector. More...

Go to the source code of this file.

Enumerations

enum  FLAG_TYPE {
  FLAG_CREATE , FLAG_MODIFY , FLAG_REMOVE , FLAG_LIST ,
  FLAG_INVALID
}
 Possible flag types. More...

Functions

FLAG_TYPE get_flag (char *flag_string)
 Assigns a string to its corresponding flag type.
const char * get_content_arg (char *str_arg)
 Safely extracts any of the non-flag arguments in the argument vector.

Detailed Description

Contains functions for extracting content from the argument vector.

Manipulates the caller buffer to extract the content.

Definition in file argparse.h.

Enumeration Type Documentation

◆ FLAG_TYPE

enum FLAG_TYPE

Possible flag types.

Enumerator
FLAG_CREATE 

-c

FLAG_MODIFY 

-m

FLAG_REMOVE 

-r

FLAG_LIST 

-l

FLAG_INVALID 

invalid/malformed flag

Definition at line 14 of file argparse.h.

15{
19 FLAG_LIST,
21} FLAG_TYPE;
FLAG_TYPE
Possible flag types.
Definition argparse.h:15
@ FLAG_REMOVE
Definition argparse.h:18
@ FLAG_LIST
Definition argparse.h:19
@ FLAG_CREATE
Definition argparse.h:16
@ FLAG_MODIFY
Definition argparse.h:17
@ FLAG_INVALID
Definition argparse.h:20

Function Documentation

◆ get_content_arg()

const char * get_content_arg ( char * str_arg)

Safely extracts any of the non-flag arguments in the argument vector.

Parameters
str_argPointer to the argument string
Returns
Pointer to the extracted argument.

Definition at line 38 of file argparse.c.

39{
40 size_t arg_len = strnlen(str_arg, MAX_CONTENT_LENGTH);
41 char* ret = (char*)malloc(arg_len+1); // +1 for null term
42 strncpy(ret, str_arg, arg_len+1);
43 return ret;
44}

◆ get_flag()

FLAG_TYPE get_flag ( char * flag_string)

Assigns a string to its corresponding flag type.

Compares the argument with the possible flags.

Parameters
flag_stringPointer to the flag string in the argument vector
Returns
Flag type corresponding to the input flag string

Definition at line 9 of file argparse.c.

10{
11 const size_t ARG_FLAG_LENGTH = strnlen(flag_string, FLAG_LENGTH);
12 if (flag_string == NULL || ARG_FLAG_LENGTH != 2)
13 {
14 printf("Flag error: empty or malformed flag (len: %zu)\n", ARG_FLAG_LENGTH);
15 return FLAG_INVALID;
16 }
17
18 if ( strncmp( flag_string, "-c", ARG_FLAG_LENGTH ) == 0 )
19 {
20 return FLAG_CREATE;
21 }
22 else if ( strncmp ( flag_string, "-r", ARG_FLAG_LENGTH) == 0 )
23 {
24 return FLAG_REMOVE;
25 }
26 else if ( strncmp ( flag_string, "-m", ARG_FLAG_LENGTH) == 0 )
27 {
28 return FLAG_MODIFY;
29 }
30 else if ( strncmp ( flag_string, "-l", ARG_FLAG_LENGTH) == 0 )
31 {
32 return FLAG_LIST;
33 }
34
35 return FLAG_INVALID;
36}