Giter Site home page Giter Site logo

cmdlinezig's Introduction

Simple command line parsing

Based on a user defined struct.

// src/test.zig
const std = @import("std");
const warn = std.debug.warn;
const clp = @import("parser.zig");

const Args = struct {
    name: []const u8 = "<name>",
    windowed: bool = false,
    width: u16 = 0,
    x: f32 = 0,
    xs: [10]i8 = [1]i8{0} ** 10,
    u8s: [2]u8 = [1]u8{0} ** 2,
};

pub fn main() anyerror!void {
    var buf: [1024 * 4]u8 = undefined;
    const a = &std.heap.FixedBufferAllocator.init(buf[0..]).allocator;

    var args = try clp.ArgParser(Args).init(a);
    defer args.available.deinit();
    try args.parse(a);

    warn("args: {}\n", args.values);
    args.show();
    warn("name provided {}\n", args.available.get("name").?.value);
}
$ zig run src/test.zig -- -xs 1 2 -u8s 1 2 3 4 --x -45 -name "my name"
args: Args{ .name = my name, .windowed = false, .width = 0, .x = -4.5e+01, .xs = i8@7fff092f7730, .u8s =  }
name: my name
x: -4.5e+01
xs: 1, 2, 0, 0, 0, 0, 0, 0, 0, 0,
u8s: 1, 2,
name provided true
$ zig build && ./zig-cache/bin/cmdlineparse -x -45
args: Args{ .name = <name>, .windowed = false, .width = 0, .x = -4.5e+01, .xs = i8@7ffe165b87a0, .u8s =  }
x: -4.5e+01
name provided false

You must provide default values for each field of the Args struct as it will be instantiated with no arguments like: Args{}.

The parser is quite lenient in what it will accept:

  • Duplicate arguments are ignored. Only the first value will be used.
  • Extra arguments are ignored.
  • Argument names are case sensitive.
  • Arguments may be provided with one or two hyphens (ie -a and --a are the same).
  • Array arguments: any number of values may be provided and only the first array.len will be applied. The rest are ignored.
  • If an argument is provided and valid, args.available.get("field_name").?.value will be set to true, otherwise false.
    • Empty array arguments are valid.

Argument supported types: Int, Float, Bool, Array, Slice of u8 (string)

Array supported types: Int, Float, Bool

TODO

  • @Cleanup free std.mem.dupe'd slices
  • Maybe support slice types other than strings

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.