Git Product home page Git Product logo

simargs's Introduction

SimArgs

Notice: this repo has moved to https://github.com/jiacai2050/zigcli.

https://github.com/jiacai2050/simargs/actions/workflows/CI.yml/badge.svg

A simple, opinionated, struct-based argument parser in Zig, taking full advantage of comptime.

Blog post explaining how simargs is implemented:

Features

  • Supported data type:
  • Optional fields and fields with default value mean they are optional arguments
  • Use comptime as possible as I can
  • Provide print_help() out of the box

Usage

See demo.zig

# Run demo
zig build && ./zig-out/bin/demo -o /tmp/a.out --user-agent Firefox hello world 2>&1
------------------------------Program------------------------------
./zig-out/bin/demo

------------------------------Arguments------------------------------
   verbose: null
user-agent: demo.main__struct_1677.main__struct_1677__enum_1777.Firefox
   timeout: 30
    output: /tmp/a.out
      help: false

------------------------------Positionals------------------------------
1: hello
2: world

------------------------------print_help------------------------------
 USAGE:
     ./zig-out/bin/demo [OPTIONS] [--] [file]

 OPTIONS:
	-v, --verbose                     Make the operation more talkative
	-A, --user-agent STRING            (valid: Chrome|Firefox|Safari)(default: Firefox)
	    --timeout INTEGER             Max time this request can cost(default: 30)
	-o, --output STRING               Write to file instead of stdout(required)
	-h, --help

Install

First add simargs to build.zig.zon of your project

.{
    .name = "..",
    .version = "..",
    .dependencies = .{
        .simargs = .{
            .url = "https://github.com/jiacai2050/simargs/archive/0a1a2afd072cc915009a063075743192fc6b1fd5.tar.gz",
            .hash = "1220a6554eccb2e9a9d7d63047e062314851ffd11315b9e6d1b5e06a9dde3275f150",
        },
    },
}

Then addModule in build.zig

const dep_simargs = b.dependency("simargs", .{
    .target = target,
    .optimize = optimize,
});
exe.addModule("simargs", dep_simargs.module("simargs"));

Finally import like this in your Zig code:

const simargs = @import("simargs");

Who’s Using

If you’d like to be added to this list, welcome to open a PR.

Acknowledgment

When implement simargs, I refer following projects to learn how to write idiomatic Zig code. Big Thanks!

simargs's People

Contributors

jiacai2050 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

simargs's Issues

Missing InvalidCmdLine on Windows

I ran simargs on Windows and it raised an error about missing InvalidCmdLine. I dig into this error in Zig and found it is windows specific. I can fixed it by adding it into ParseError like this:

const ParseError = error{ NoProgram, NoOption, MissingRequiredOption, MissingOptionValue, InvalidEnumValue, InvalidCmdLine };

Not sure it is the correct solution, though.

"comptime inline for" seems not necessary

In line 57 in simargs.zig

comptime inline for (std.meta.fields(shorts_type)) |fld| {
 // ...
};

can be just

inline for (std.meta.fields(shorts_type)) |fld| {....}

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.