Skip to content

Commit

Permalink
#16 formatting and addition of .clangd
Browse files Browse the repository at this point in the history
  • Loading branch information
likle committed Apr 20, 2024
1 parent 6535721 commit 393f5e8
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 44 deletions.
34 changes: 34 additions & 0 deletions .clangd
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
CompileFlags:
Add:
- "-I../include"
- "-xc"
- "-std=c11"
Diagnostics:
UnusedIncludes: Strict
MissingIncludes: Strict

ClangTidy:
Add:
- bugprone-*
- cert-*
- clang-*
- concurrency-*
- google-*
- misc-*
- modernize-*
- performance-*
- portability-*
- readability-*
Remove:
- misc-no-recursion
- readability-isolate-declaration
- readability-identifier-length
- readability-else-after-return
- readability-magic-numbers
- readability-uppercase-literal-suffix
- readability-function-cognitive-complexity
- google-readability-todo
- bugprone-sizeof-expression
- bugprone-easily-swappable-parameters
- bugprone-switch-missing-default-case
- modernize-macro-to-enum
6 changes: 4 additions & 2 deletions demo/main.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include <cargs.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

static struct cag_option options[] = {
{.identifier = 's',
{.identifier = 's',
.access_letters = "s",
.access_name = NULL,
.value_name = NULL,
Expand All @@ -30,7 +31,8 @@ static struct cag_option options[] = {
{.identifier = 'h',
.access_letters = "h",
.access_name = "help",
.description = "Shows the command help"}};
.description = "Shows the command help"},
};

int main(int argc, char *argv[])
{
Expand Down
46 changes: 25 additions & 21 deletions include/cargs.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ typedef struct cag_option_context
} cag_option_context;

/**
* Prototype for printer used in cag_option_printer. For example fprintf have same prototype
* Prototype for printer used in cag_option_printer. For example fprintf have
* same prototype
*/
typedef int (*cag_printer)(void *ctx, const char *fmt, ...);

Expand Down Expand Up @@ -172,7 +173,8 @@ CAG_PUBLIC int cag_option_get_index(const cag_option_context *context);
*
* This function retrieves the index of an invalid option if the provided option
* does not match any of the options specified in the `cag_option` list. This is
* particularly useful when detailed information about an invalid option is required.
* particularly useful when detailed information about an invalid option is
* required.
*
* @param context Pointer to the context from which the option was fetched.
* @return Returns the index of the invalid option, or -1 if it is not invalid.
Expand All @@ -192,14 +194,16 @@ CAG_PUBLIC int cag_option_get_error_index(const cag_option_context *context);
CAG_PUBLIC char cag_option_get_error_letter(const cag_option_context *context);

/**
* @brief Prints the error associated with the invalid option to the specified destination.
* @brief Prints the error associated with the invalid option to the specified
* destination.
*
* This function prints information about the error associated with the invalid option
* to the specified destination (such as a file stream). It helps in displaying the error
* of the current context.
* This function prints information about the error associated with the invalid
* option to the specified destination (such as a file stream). It helps in
* displaying the error of the current context.
*
* @param context Pointer to the context from which the option was fetched.
* @param destination Pointer to the file stream where the error information will be printed.
* @param destination Pointer to the file stream where the error information
* will be printed.
*/
#ifndef CAG_NO_FILE
CAG_PUBLIC void cag_option_print_error(const cag_option_context *context,
Expand Down Expand Up @@ -237,20 +241,20 @@ CAG_PUBLIC void cag_option_print(const cag_option *options, size_t option_count,
FILE *destination);
#endif

/**
* @brief Prints all options using user callback.
*
* This function prints all options using user callback. This can be used to
* generate the output for a "--help" option.
* Using user callback is useful in tiny system without FILE support
*
* @param options The options which will be printed.
* @param option_count The option count which will be printed.
* @param destination The destination where the output will be printed.
* @param printer The printer callback function. For example fprintf.
* @param printer_ctx The parameter for printer callback. For example fprintf
* could use parameter stderr.
*/
/**
* @brief Prints all options using user callback.
*
* This function prints all options using user callback. This can be used to
* generate the output for a "--help" option.
* Using user callback is useful in tiny system without FILE support
*
* @param options The options which will be printed.
* @param option_count The option count which will be printed.
* @param destination The destination where the output will be printed.
* @param printer The printer callback function. For example fprintf.
* @param printer_ctx The parameter for printer callback. For example fprintf
* could use parameter stderr.
*/
CAG_PUBLIC void cag_option_printer(const cag_option *options,
size_t option_count, cag_printer printer, void *printer_ctx);

Expand Down
44 changes: 23 additions & 21 deletions src/cargs.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <assert.h>
#include <cargs.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

Expand Down Expand Up @@ -38,6 +39,7 @@ static void cag_option_print_name(const cag_option *option, bool *first,
if (option->access_name != NULL) {
if (*first) {
*accessor_length += printer(printer_ctx, "--%s", option->access_name);
*first = false;
} else {
*accessor_length += printer(printer_ctx, ", --%s", option->access_name);
}
Expand Down Expand Up @@ -262,11 +264,11 @@ static void cag_option_shift(cag_option_context *context, int start, int option,
char *tmp;
int a_index, shift_index, left_shift, right_shift, target_index, source_index;

// The block between start and option will be shifted to the end, and the order
// of everything will be preserved. Left shift is the amount of indexes the block
// between option and end will shift towards the start, and right shift is the
// amount of indexes the block between start and option will be shifted towards
// the end.
// The block between start and option will be shifted to the end, and the
// order of everything will be preserved. Left shift is the amount of indexes
// the block between option and end will shift towards the start, and right
// shift is the amount of indexes the block between start and option will be
// shifted towards the end.
left_shift = option - start;
right_shift = end - option;

Expand Down Expand Up @@ -462,22 +464,6 @@ CAG_PUBLIC void cag_option_printer_error(const cag_option_context *context,
}
}

#ifndef CAG_NO_FILE
CAG_PUBLIC void cag_option_print_error(const cag_option_context* context,
FILE* destination)
{
cag_option_printer_error(context, (cag_printer)fprintf, destination);
}
#endif

#ifndef CAG_NO_FILE
void cag_option_print(const cag_option* options, size_t option_count,
FILE* destination)
{
cag_option_printer(options, option_count, (cag_printer)fprintf, destination);
}
#endif

CAG_PUBLIC void cag_option_printer(const cag_option *options,
size_t option_count, cag_printer printer, void *printer_ctx)
{
Expand Down Expand Up @@ -508,6 +494,22 @@ CAG_PUBLIC void cag_option_printer(const cag_option *options,
}
}

#ifndef CAG_NO_FILE
CAG_PUBLIC void cag_option_print_error(const cag_option_context *context,
FILE *destination)
{
cag_option_printer_error(context, (cag_printer)fprintf, destination);
}
#endif

#ifndef CAG_NO_FILE
void cag_option_print(const cag_option *options, size_t option_count,
FILE *destination)
{
cag_option_printer(options, option_count, (cag_printer)fprintf, destination);
}
#endif

void cag_option_prepare(cag_option_context *context, const cag_option *options,
size_t option_count, int argc, char **argv)
{
Expand Down

0 comments on commit 393f5e8

Please sign in to comment.