• Stars
    star
    110
  • Rank 314,932 (Top 7 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 1 year ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Code generation tool, creates python / C programs that parse command line arguments. Say bye to starting each project by reading "argparse" / "geptopt.h" docs.

duckargs 🦆

tests_badge cov_badge version_badge license_badge downloads_badge conda_badge

Brief description

The purpose of duckargs is to save some typing whenever you want to quickly create a python program or C program that accepts command line arguments. Just run duckargs (generates python), duckargs-python (also generates python) or duckargs-c (generates C) with all the options/arguments that you want your program to accept, and duckargs will print the code for a program that handles those options/arguments.

Longer description

If you're like me, then you often need to create little throwaway command-line tools, but not often enough to remember the exact syntax/details of argparse or getopt.h, so you often start these efforts by looking up the relevant docs and refreshing your memory.

Next, you spend some time typing out the boilerplate arg-parsing code, with one eye on the docs, and eventually (depending on how much arg-parsing boilerplate code you need) you may forget some interesting detail that was part of your original idea, or you may just get sick of it and decide that you don't event need a command-line tool, and you'll do the thing manually instead.

duckargs makes this process a little bit simpler, and shortens the time between your idea and having a working C or Python program.

Let's imagine that you want to create a little command-line tool that accepts the following command line options/arguments:

  • A positional argument, string
  • An optional integer value (-i or --intval)
  • An optional float value (-f or --floatval)
  • A flag (-q)

You can run duckargs and pass all those options/arguments/flags, and duckargs will generate a working program with all the boilerplate taken care of:

Generating Python

$ duckargs somestring -i --intval 99 -f --floatval 7.7 -q

Output

# Generated by duckargs, invoked with the following arguments:
# somestring -i --intval 99 -f --floatval 7.7 -q

import argparse

def main():
    parser = argparse.ArgumentParser(description='A command-line program generated by duckargs',
                                     formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument('somestring', help='a string')
    parser.add_argument('-i', '--intval', default=99, type=int, help='an int value')
    parser.add_argument('-f', '--floatval', default=7.7, type=float, help='a float value')
    parser.add_argument('-q', action='store_true', help='q flag')
    args = parser.parse_args()

    print(args.somestring)
    print(args.intval)
    print(args.floatval)
    print(args.q)

if __name__ == "__main__":
    main()

Generating C

$ duckargs-c somestring -i --intval 99 -f --floatval 7.7 -q

Output

// Generated by duckargs, invoked with the following arguments:
// somestring -i --intval 99 -f --floatval 7.7 -q

#include <stdbool.h>
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>

static char *somestring = "somestring";
static long int intval = 99;
static float floatval = 7.7;
static bool q = false;

static struct option long_options[] =
{
    {"intval", required_argument, NULL, 'i'},
    {"floatval", required_argument, NULL, 'f'},
    {NULL, 0, NULL, 0}
};

void print_usage(void)
{
    printf("\n");
    printf("USAGE:\n\n");
    printf("program_name [OPTIONS] somestring\n");
    printf("\nOPTIONS:\n\n");
    printf("-i --intval [int]      An int value (default: %ld)\n", int);
    printf("-f --floatval [float]  A float value (default: %.2f)\n", float);
    printf("-q                     A flag\n");
    printf("\n");
}

int parse_args(int argc, char *argv[])
{
    char *endptr = NULL;
    int ch;

    while ((ch = getopt_long(argc, argv, "i:f:q", long_options, NULL)) != -1)
    {
        switch (ch)
        {
            case 'i':
            {
                intval = strtol(optarg, &endptr, 0);
                if (endptr && (*endptr != '\0'))
                {
                    printf("Option '-i' requires an integer argument\n");
                    return -1;
                }
                break;
            }
            case 'f':
            {
                floatval = strtof(optarg, &endptr);
                if (endptr == optarg)
                {
                    printf("Option '-f' requires a floating-point argument\n");
                    return -1;
                }
                break;
            }
            case 'q':
            {
                q = true;
                break;
            }
        }
    }

    if (argc < (optind + 1))
    {
        printf("Missing positional arguments\n");
        return -1;
    }

    somestring = argv[optind];

    return 0;
}

int main(int argc, char *argv[])
{
    if (argc < 2)
    {
        print_usage();
        return -1;
    }

    int ret = parse_args(argc, argv);
    if (0 != ret)
    {
        return ret;
    }

    printf("somestring: %s\n", somestring ? somestring : "null");
    printf("intval: %ld\n", intval);
    printf("floatval: %.4f\n", floatval);
    printf("q: %s\n", q ? "true" : "false");

    return 0;
}

Install

Install with pip (python 3x required):

pip install duckargs

Comma-separated choices for option argument

If you have an option which accepts an argument, and you write an argument string with multiple values separated by commas (e.g. -m --mode active,idle,sim), then generated python code will use the comma-separated values as a choices list for argparse:

parser.add_argument('-m', '--mode', choices=['active', 'idle', 'sim'], default='active', help='a string')

And generated C code will use the comma-separated values to restrict values in a similar manner:

static char *mode_choices[] = {"active", "idle", "stop"};
static char *mode = "active";

static struct option long_options[] =
{
    {"mode", required_argument, NULL, 'm'},
    {NULL, 0, NULL, 0}
};

void print_usage(void)
{
    printf("\n");
    printf("USAGE:\n\n");
    printf("program_name [OPTIONS]\n");
    printf("\nOPTIONS:\n\n");
    printf("-m --mode [active|idle|stop]  A string value (default: %s)\n", mode ? mode : "null");
    printf("\n");
}

int parse_args(int argc, char *argv[])
{
    int ch;

    while ((ch = getopt_long(argc, argv, "m:", long_options, NULL)) != -1)
    {
        switch (ch)
        {
            case 'm':
            {
                mode = optarg;
                for (int i = 0; i < 3; i++)
                {
                    if (0 == strcmp(mode_choices[i], mode))
                    {
                        break;
                    }
                    if (i == 2)
                    {
                        printf("Option '-m' must be one of ['active', 'idle', 'stop']\n");
                        return -1;
                    }
                }
                break;
            }
        }
    }

    return 0;
}

Filenames for option arguments

If you have an option that you want to accept a filename, you have two ways to tell duckargs that the option argument should be treated as a file:

  • Pass the path to a file that actually exists (e.g. -f --filename file.txt) as the option argument
  • Pass FILE as the option argument (e.g. -f --filename FILE)

Either of which will generate python code like this:

parser.add_argument('-f', '--filename', default='file', type=argparse.FileType(), help='a filename')

And will generate C code like this:

static char *filename = NULL;

static struct option long_options[] =
{
    {"filename", required_argument, NULL, 'f'},
    {NULL, 0, NULL, 0}
};

void print_usage(void)
{
    printf("\n");
    printf("USAGE:\n\n");
    printf("program_name [OPTIONS]\n");
    printf("\nOPTIONS:\n\n");
    printf("-f --filename FILE  A filename (default: %s)\n", filename ? filename : "null");
    printf("\n");
}

int parse_args(int argc, char *argv[])
{
    int ch;

    while ((ch = getopt_long(argc, argv, "f:", long_options, NULL)) != -1)
    {
        switch (ch)
        {
            case 'f':
            {
                filename = optarg;
                break;
            }
        }
    }

    return 0;
}

Environment variables

Some things can be configured by setting environment variables.

DUCKARGS_PRINT

By default, duckargs generates a program that prints all provided arguments/options to stdout after argument parsing is complete. If you want to disable this and generate programs without the print statements, set DUCKARGS_PRINT=0 in your environment variables. This environment variable affects generated C code and generated python code.

DUCKARGS_COMMENT

By default, duckargs generates a program that prints a comment header at the top, showing the arguments that duckargs was invoked with. If you want to disable this and generate programs without the comment header, set DUCKARGS_COMMENT=0 in your environment variables. This environment variable affects generated C code and generated python code.

Use duckargs in python code

If you want to use duckargs in your own script, you can use the duckargs.generate_python_code and duckargs.generate_c_code functions, both of which accept a list of command line arguments:

import sys
from duckargs import generate_python_code, generate_c_code

python_code = generate_python_code(sys.argv)

c_code = generate_c_code(sys.argv)

Pitfalls

If you have a combination of flags and positional arguments, and you happen to have a flag followed by a positional argument (as in: python -m duckargs -q --quiet positional_arg), duckargs has no way to tell that you wanted a positional arg, so it will assume you want an option -q --quiet with a required argument.

To avoid this, it is recommended to declare your positional arguments first (as in: python -m duckargs positional_arg -q --quiet)

Contributions

Contributions are welcome, please open a pull request at https://github.com/eriknyquist/duckargs/pulls. You will need to install packages required for development by doing pip install -r dev_requirements.txt.

Please ensure that all existing tests pass, new test(s) are added if required, and the code coverage check passes.

  • Run tests with python setup.py test.
  • Run tests and and generate code coverage report with python code_coverage.py (this script will report an error if coverage is below 95%)

If you have any questions about / need help with contributions or tests, please contact Erik at [email protected].

More Repositories

1

generate_life_calendar

Generate a personalized life calendar poster
Python
125
star
2

librxvm

non-backtracking NFA-based regular expression library, for C and Python
C
61
star
3

boyermoore

Boyer-moore in pure python, search for unicode strings in large files quickly
Python
20
star
4

tones

Pure-python library for generating audio tones, with support for polyphony, pitch-bending and vibrato
Python
17
star
5

ptttl

Polyphonic Tone Transfer Language: superset of NOKIA's RTTTL, supports polyphony & vibrato
C
14
star
6

text_game_maker

Python framework for creating text-based games (interactive fiction)
Python
12
star
7

personal-site

HTML
11
star
8

nedry

Nedry is a fun & useful discord bot with a modular plugin system (formerly "twitch_monitor_discord_bot")
Python
10
star
9

deep_space_trader

Deep Space Trader is a turn-based strategy game inspired by the old DOS game "Drugwars".
Python
9
star
10

versionedobj

Easy object serialization and versioning framework for python
Python
9
star
11

BrainfuckIntern

Uses a genetic algorithm to "evolve" brainfuck programs with desirable behaviours
C
9
star
12

app_timer

Friendly, lightweight, hardware-agnostic timer abstraction layer in C. Use a single timer/counter source to drive many timed events.
C
8
star
13

bfi

Fast optimizing Brainfuck interpreter in pure python
Brainfuck
8
star
14

chatbot_utils

RegEx-based fuzzy command / response handling for conversational chatbots
Python
4
star
15

keystroke_transcriber

Records global keypress events and translates them into a program that replays those keypress events on some programmable USB HID device (e.g. Digispark)
Python
3
star
16

wikichangewatcher

Python library for easy monitoring of all Wikipedia page edits
Python
3
star
17

hashtable

A lightweight separate-chaining arena-backed hashtable in C, designed to be flexible enough for embedded systems
C
3
star
18

ulist

Unrolled linked list implementation in C
C++
2
star
19

parse_args

Portable command-line argument parser for C programs
C
2
star
20

cheaders

C header file generator
Python
2
star
21

zombiegame

C#
1
star
22

trivia_discord_bot

A discord bot for group trivia contests
Python
1
star
23

addlad

A single-instruction esoteric programming language, inspired by Chris Domas' "MovFuscator"
Python
1
star
24

varints

Variable-length integer (VLQ) encoding and decoding, in C99. https://en.wikipedia.org/wiki/Variable-length_quantity
C
1
star
25

arduinozero-metronome

Stage metronome for drummers, using Arduino Zero
C
1
star
26

twitch_talkbot

Python
1
star
27

snakeng

Implementation of the classic "snake" game as a re-usable engine. Play snake on the command line, or implement your own!
Python
1
star
28

google_drive_files

Download/list files from Google Drive by filename
Python
1
star
29

versioned_config

Python library for creating and managing versioned configuration files
Python
1
star
30

chatbot_builder

A discord chat bot that is programmed interactively by discord users
Python
1
star
31

ArduinoSoftwarePinInterrupts

This arduino library implements a software-polling-based version of the "attachInterrupt" function, which supports all digital pins on all hardware types, supports attaching multiple handlers to a single pin, and also has built-in optional debouncing for your input signals.
C++
1
star