All Projects → mysteriouspants → Argumentparser

mysteriouspants / Argumentparser

Licence: other
Faster, easier, more declarative parsing of command line arguments in Objective-C/Foundation.

Projects that are alternatives of or similar to Argumentparser

Caporal.js
A full-featured framework for building command line applications (cli) with node.js
Stars: ✭ 3,279 (+1206.37%)
Mutual labels:  command-line, command-line-parser, argument-parser, argument-parsing
Clap
Create your command-line parser, with all of the bells and whistles, declaratively or procedurally.
Stars: ✭ 7,174 (+2758.17%)
Mutual labels:  command-line, command-line-parser, argument-parser, argument-parsing
Clikt
Multiplatform command line interface parsing for Kotlin
Stars: ✭ 1,658 (+560.56%)
Mutual labels:  command-line, command-line-parser, argument-parser, argument-parsing
Clipp
easy to use, powerful & expressive command line argument parsing for modern C++ / single header / usage & doc generation
Stars: ✭ 687 (+173.71%)
Mutual labels:  command-line, argument-parser, argument-parsing
Argh
Argh! A minimalist argument handler.
Stars: ✭ 752 (+199.6%)
Mutual labels:  command-line, command-line-parser, argument-parser
declarative-parser
Modern, declarative argument parser for Python 3.6+
Stars: ✭ 31 (-87.65%)
Mutual labels:  argument-parser, argument-parsing, command-line-parser
Picocli
Picocli is a modern framework for building powerful, user-friendly, GraalVM-enabled command line apps with ease. It supports colors, autocompletion, subcommands, and more. In 1 source file so apps can include as source & avoid adding a dependency. Written in Java, usable from Groovy, Kotlin, Scala, etc.
Stars: ✭ 3,286 (+1209.16%)
Mutual labels:  command-line, command-line-parser, argument-parsing
Argparse.jl
Package for parsing command-line arguments to Julia programs.
Stars: ✭ 131 (-47.81%)
Mutual labels:  command-line, argument-parser, argument-parsing
Nclap
NClap is a .NET library for parsing command-line arguments and building interactive command shells. It's driven by a declarative attribute syntax, and easy to extend.
Stars: ✭ 51 (-79.68%)
Mutual labels:  command-line-parser, argument-parsing
Conf
Go package for loading program configuration from multiple sources.
Stars: ✭ 70 (-72.11%)
Mutual labels:  command-line, command-line-parser
Argumentum
C++ command line parsing library
Stars: ✭ 92 (-63.35%)
Mutual labels:  command-line, argument-parser
Andhow
Strongly typed, validated, easy to use Java configuration
Stars: ✭ 17 (-93.23%)
Mutual labels:  command-line, command-line-parser
Yaap
Yet Another (Swift) Argument Parser
Stars: ✭ 124 (-50.6%)
Mutual labels:  argument-parser, argument-parsing
Programoptions.hxx
Single-header program options parsing library for C++11
Stars: ✭ 112 (-55.38%)
Mutual labels:  command-line-parser, argument-parser
Typin
Declarative framework for interactive CLI applications
Stars: ✭ 126 (-49.8%)
Mutual labels:  command-line, command-line-parser
Spectre.cli
An extremely opinionated command-line parser.
Stars: ✭ 121 (-51.79%)
Mutual labels:  command-line, command-line-parser
Commander
🚀The framework to write type-safe and structured command line program easily in Swift.
Stars: ✭ 170 (-32.27%)
Mutual labels:  command-line, command-line-parser
Deno Cliffy
Command line framework for deno 🦕 Including Commandline-Interfaces, Prompts, CLI-Table, Arguments Parser and more...
Stars: ✭ 149 (-40.64%)
Mutual labels:  command-line, argument-parser
Cli Matic
Compact, hands-free [sub]command line parsing library for Clojure.
Stars: ✭ 245 (-2.39%)
Mutual labels:  command-line, command-line-parser
Clamp
a Ruby command-line application framework
Stars: ✭ 387 (+54.18%)
Mutual labels:  command-line, command-line-parser

XPMArgumentParser

A short, awesome, and really useful tool for rapidly parsing command-line arguments in a declarative manner using Objective-C.

XPMArgumentSignature
  * force = [XPMArgumentSignature argumentSignatureWithFormat:@"[-f --force]"],
  * soft = [XPMArgumentSignature argumentSignatureWithFormat:@"[-s --soft]"],
  * outputFile = [XPMArgumentSignature argumentSignatureWithFormat:@"[-o --output-file of]=",
  * inputFile = [XPMArgumentSignature argumentSignatureWithFormat:@"[-i --input-file if]={1,}"];
  
NSArray * signatures = @[force, soft, outputFile, inputFile];

XPMArgumentPackage * package =
 [[NSProcessInfo currentProcess] xpmargs_parseArgumentsWithSignatures:signatures];

if ([package booleanValueOfSignature:soft]) {
    // presumably you'd do something
}

if ([package firstObjectForSignature:inputFile]) {
    printf("dude, you gotta specify a file!\n");
    return -1;
}

Updates

2016

  • Removed CoreParse as a dependency, making it easier to include this project for everyone.
  • Reprefixed away from reserved prefix for filesystem work, now using XPM prefix. Should be a simple find/replace to update.

Features: It Just Works

You're probably already excited about the nice format ctors, which are admittedly a nice touch. But the real power of XPMArgumentParser is on the command line. It's designed to "just work" in a variety of situations.

Flag Grouping

It seems natural to us: we like to group flags together. tar -cvvf, anyone? XPMArgumentParser understands that quite well.

Equals Signs

Some tools require an equals sign for value assignment (foo -f=t works, but foo -f t doesn't). XPMArgumentParser doesn't mind either formats.

Multiple Values in a Group

Supposing you have two or more flags that require values, how does that work? XPMArgumentParser gives you two ways to work:

    # the long way
spiffy -i file1 -o file2
    # the lazy way
spiffy -io file1 file2

Personally, I prefer the lazy way. The values are assigned respective to the order of the flags in the group. Note that equals signs are not really logical in argument groups. If you do something like -cfg=file.txt, it will assign file.txt to f (if that's the first flag that takes a value), but it will not do anything fancy, like force that flag to take only one value if it supports multiple values. For that you need to use a barrier.

Many Values per Argument

New in this version is the ability to have more than one value per time an argument is invoked. You define the number of arguments per invocation as a range, minimum to maximum.

XPMArgumentSignature * files =
    [XPMArgumentSignature argumentSignatureWithFormat:@"[-f --files]={1,5}"];

And boom, you can specify between one and five files per time you use the -f flag. You might think that this could be a little awkward if you have a flag group with two flags that take multiple arguments. Well, it isn't. XPMArgumentParser understands "value barriers," which segregate between lists of values. A value barrier is either two dashes (--), or any other kind of argument invocation. So, given the following:

XPMArgumentSignature
  *inFiles = [XPMArgumentSignature argumentSignatureWithFormat:@"[-f --input-files]={1,5}"],
  *outputFiles = [XPMArgumentSignature argumentSignatureWithFormat:@"[-o --output-files]={1,5}"];

// on the command line:

foo -ofv ouput1 output2 output3 -- input1 input2 input3 input4 # use the double-dash to separate
foo -of ouput1 output2 output3 -v input1 input2 input3 input4 # use the verbose flag to separate

See how it "just works" for you?

Undecorated Arguments

Who here is in love with how the dd utility takes its arguments? So perhaps not many, but XPMArgumentParser understands that, too.

foo if=infile of=outfile

Is perfectly valid. This can also be used to create "subcommands."

foo commit -Am "Why are you reinventing git?"

Argument Injection

Wouldn't it be nice to build out a tree of possible arguments, with some arguments which are scanned if and only if a certain argument is present? So thought I, which is why the following invocation could be created like this:

foo commit -Am "Why are you reinventing git?"

// can be accomplished with

XPMArgumentSignature * commitSubcommand =
  [XPMArgumentSignature argumentSignatureWithFormat:@"[commit]"];
[commitSubcommand setInjectedSignatures:[NSSet setWithObjects:
  [XPMArgumentSignature argumentSignatureWithFormat:@"[-A --all]"],
  [XPMArgumentSignature argumentSignatureWithFormat:@"[-m --commit-message]="], nil]];

Descriptions

By default the -description method returns a very simple programmer-friendly text. However, you can use the descriptionHelper block property on XPMArgumentSignature. A different description method which you can call for emitting command-line help will use this. For example:

XPMArgumentSignature * verbose = [XPMArgumentSignature argumentSignatureWithFormat:@"[-v --verbose]"];
XPMArgumentSignature * help = [XPMArgumentSignature argumentSignatureWithFormat:@"[-h --help]"];

[verbose setDescriptionHelper:(NSString *)(^)(XPMArgumentSignature * currentSignature, NSUInteger indentLevel, NSUInteger terminalWidth) {
    return [@"-v --verbose  Emit more information." xpmargs_mutableStringByIndentingToWidth:indentLevel * 2 lineLength:terminalWidth];
}];
[help setDescriptionHelper:(NSString *)(^)(XPMArgumentSignature * currentSignature, NSUInteger indentLevel, NSUInteger terminalWidth) {
    return [@"-h --help     Show this message." xpmargs_mutableStringByIndentingToWidth:indentLevel * 2 lineLength:terminalWidth];
}];

XPMArgumentPackage * package = 
 [[NSProcessInfo processInfo] xpmargs_parseArgumentsWithSignatures:[NSSet setWithObjects:verbose, help, nil]];

if ([package booleanValueOfFlag:help]) {
    struct winsize ws;
    ioctl(0, TIOCGWINSZ, &ws);

    printf("My Really Cool CLI Tool v0.1\n\n");
    printf("%s\n", [[verbose descriptionForHelpWithIndent:2 width:ws.ws_col] UTF8String]);
    printf("%s\n", [[help descriptionForHelpWithIndent:2 width:ws.ws_col] UTF8String]);
    printf("\n(C) 2012 by Your Face. All your base are belong to us.\n");
}

Alternatives

  • BRLOptionParser - An Objective-C wrapper for getopt_long. Looks to be well-constructed.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].