~brenns10/sc-argparse

0923442e5cde146139ec43c2b5dafd81abcdc37e — Stephen Brennan 4 years ago 92ec9c0
Add typical sc- project files and build system
8 files changed, 133 insertions(+), 37 deletions(-)

A .clang-format
A .gitignore
A .pre-commit-config.yaml
A LICENSE
A compile_commands.json
A meson.build
A src/example.c
M src/sc-argparse.c
A .clang-format => .clang-format +12 -0
@@ 0,0 1,12 @@
BasedOnStyle: LLVM
IndentWidth: 8
UseTab: ForIndentation
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
IndentCaseLabels: false
ContinuationIndentWidth: 8
Cpp11BracedListStyle: false
AlignConsecutiveMacros: true
BraceWrapping:
  AfterEnum: true

A .gitignore => .gitignore +2 -0
@@ 0,0 1,2 @@
build
.clangd

A .pre-commit-config.yaml => .pre-commit-config.yaml +12 -0
@@ 0,0 1,12 @@
repos:
-   repo: local
    hooks:
    -   id: clang-format
        name: "Run clang-format"
        language: system
        files: "^.*\\.[ch]$"
        entry: clang-format -i
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v2.5.0
    hooks:
    -   id: trailing-whitespace

A LICENSE => LICENSE +24 -0
@@ 0,0 1,24 @@
Copyright (c) 2020 Stephen Brennan. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its
       contributors may be used to endorse or promote products derived from
       this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

A compile_commands.json => compile_commands.json +1 -0
@@ 0,0 1,1 @@
build/compile_commands.json
\ No newline at end of file

A meson.build => meson.build +41 -0
@@ 0,0 1,41 @@
project(
  'sc-argparse', 'c',
  version : '0.0.0',
)

sources = [
  'src/sc-argparse.c',
]

inc = include_directories('include')

libsc_argparse = library(
  'sc-argparse',
  sources,
  include_directories : inc,
  install : true,
)

libsc_argparse_dep = declare_dependency(
  include_directories : inc,
  link_with : libsc_argparse,
)

pkg = import('pkgconfig')
pkg.generate(
  libraries: libsc_argparse,
  subdirs: 'include',
  version: meson.project_version(),
  name: 'libsc-argparse',
  filebase: 'sc-argparse',
  description: 'Simple argument parsing library',
)

executable(
  'argument-tester',
  ['src/example.c'],
  dependencies: libsc_argparse_dep,
)

# For each public header in "include/":
install_headers('include/sc-argparse.h', subdir : 'sc-argparse')

A src/example.c => src/example.c +41 -0
@@ 0,0 1,41 @@
/**
 * example.c: Example of using the sc-argparse library
 */
#include <stdio.h>

#include "sc-argparse.h"

static void printargs(int argc, char **argv, char *pfx)
{
	int i;
	for (i = 0; i < argc; i++)
		printf("%s[%d] = %s\n", pfx, i, argv[i]);
}

enum my_args {
	ARG_HOST = 0,
	ARG_PORT,
	ARG_VERBOSE,
	NUM_ARGS,
};

int main(int argc, char **argv)
{
	struct sc_arg args[NUM_ARGS];
	int rv;
	args[ARG_HOST] = SC_ARG_STRING('H', "--host", "host to connect");
	args[ARG_PORT] = SC_ARG_DEF_INT('p', "--port", 80, "port to connect");
	args[ARG_VERBOSE] = SC_ARG_COUNT('v', "--verbose", "print more");
	args[NUM_ARGS - 1] = SC_ARG_END();

	if ((rv = sc_argparse(args, argc - 1, argv + 1)) >= 0) {
		printf("Parsing successful!\n");
		printf("Host: %s\n", args[ARG_HOST].val_string);
		printf("Port: %d\n", args[ARG_PORT].val_int);
		printf("Verbose: %d\n", args[ARG_VERBOSE].val_int);
		printf("Posargs (%d):\n", rv);
		printargs(rv, argv + 1, "posargs");
	} else {
		printf("error\n");
	}
}

M src/sc-argparse.c => src/sc-argparse.c +0 -37
@@ 4,7 4,6 @@

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "sc-argparse.h"



@@ 173,7 172,6 @@ int sc_argparse(struct sc_arg argspec[], int argc, char **argv)
		} else if (err == STOP_PROCESSING) {
			break;
		} else {
			printf("ERROR: %d\n", err);
			return -1;
		}
	}


@@ 183,38 181,3 @@ int sc_argparse(struct sc_arg argspec[], int argc, char **argv)

	return state.pos;
}

static void printargs(int argc, char **argv, char *pfx)
{
	int i;
	for (i = 0; i < argc; i++)
		printf("%s[%d] = %s\n", pfx, i, argv[i]);
}

enum my_args {
	ARG_HOST=0,
	ARG_PORT,
	ARG_VERBOSE,
	NUM_ARGS,
};

int main(int argc, char **argv)
{
	struct sc_arg args[NUM_ARGS];
	int rv;
	args[ARG_HOST] = SC_ARG_STRING('H', "--host", "host to connect");
	args[ARG_PORT] = SC_ARG_DEF_INT('p', "--port", 80, "port to connect");
	args[ARG_VERBOSE] = SC_ARG_COUNT('v', "--verbose", "print more");
	args[NUM_ARGS-1] = SC_ARG_END();

	if ((rv = sc_argparse(args, argc-1, argv+1)) >= 0) {
		printf("Parsing successful!\n");
		printf("Host: %s\n", args[ARG_HOST].val_string);
		printf("Port: %d\n", args[ARG_PORT].val_int);
		printf("Verbose: %d\n", args[ARG_VERBOSE].val_int);
		printf("Posargs (%d):\n", rv);
		printargs(rv, argv+1, "posargs");
	} else {
		printf("error\n");
	}
}