Latest 0.1.0
License MIT
Platforms osx 10.10


A command line arguments parser of Objective-C


See Magic-Unique/MobileProvisionTool


  1. Support subcommands
  2. Support Queries
    • key-value (require)
    • key-value (optional)
    • key-value (optional & default-if-nil)
  3. Support Flags
  4. Support Abbr and multi-abbrs parsing
  5. Auto create colorful help infomation (just like cocoapods.)
  6. Auto print helping infomation if arguments is invalid
  7. Version command
  8. Output with verbose/success/warning/error/info
  9. Custom colorful text



pod 'CommandLine'


Drag CommandLine folder to your project.


#import "CommandLine.h"



If you want to define the command like:

$ pod spec create

it’s meaning:

binary command subcommand subsubcommand…
pod spec create

you can execute the code before parse.

CLCommand *pod = [CLCommand main];
CLCommand *spec = [pod defineSubcommand:@"spec"];
spec.explain = @"Spec commands"
    CLCommand *create = [spec defineSubcommand:@"create"];
    create.explain = @"Create a pod spec";
    [create onHandlerRequest:^CLResponse *(CLCommand *command, CLRequest *request) {
        // do something to create a cocoapods spec.

        // return an error or an userInfo for succeed.
        return [CLResponse succeed:nil];


If you want to define the command like:

$ codesign [--entitlement /path/to/entitlement.plist] --cert "iPhone Developer: XXXX" ...
# or
$ codesign [-e /path/to/entitlement.plist] -c "iPhone Developer: XXXX" ...

It’s meaning:

Binary Query Key 1 Query Value 1 Query Key 2 Query Value 2
codesign entitlement/e (optional) /path/to/entitlement.plist cert/c (require) Cert Name

you can execute the code before parse.

CLCommand *codesign = [CLCommand main]; // get main command (without any command or subcommands)
    .setExplain("Entitlement.plist file path."); // define a optional query
    .setExplain("Cert name"); // define a require query
[codesign onHandlerRequest:^CLResponse *(CLCommand *command, CLRequest *request) {
    NSString *cert = request.queries[@"cert"]; // get value with key.
    NSString *entitlement = request.queries[@"entitlement"]; // nonable
    //  to code sign

    return [CLResposne succeed:nil];


If you want to define the command like:

$ ls --all
# or
$ ls -a

It’s meaning:

Binary Flag Key
ls all / a

you can execute the code before parse.

CLCommand *ls = [CLCommand main]; // get main command (without any command or subcommands)
    .setExplain(@"Print all contents."); // define a optional query
[ls onHandlerRequest:^CLResponse *(CLCommand *command, CLRequest *request) {
    BOOL all = [request.flags containsObject:@"all"];

    // list and print
    NSFileManager *fmgr = [NSFileManager defaultManager];
    NSError *error = nil;
    NSArray *contents = [fmgr contentsOfDirectory:[CLIOPath currentDirectory] error:&error];
    if (error) {
        printf("%sn", error.localizedDescription.UTF8String);
        return [CLResponse error:error];
    if (NO == all) {
        NSMutableArray *mContents = [NSMutableArray arrayWithArray:contents];
        //  remove all item with "." prefix in mContents;
        contents = ;mContents copy];
    for (NSString *item in contents) {
        printf("%sn", item.UTF8String);
    return [CLResponse success:nil];

Abbr & Multi-abbrs

For example:

# Multi-abbrs for flags:

$ rm -rf /path/to/directory

# is meaning:

$ rm -r -f /path/to/directory
$ rm --recursive --force /path/to/directory

# 'r' is recursive(flag)'s abbr, 'f' is force(flag)'s abbr.
# Multi-abbrs for flags and a query

$ codesign -fs 'iPhone Developer: XXXX (XXXX)' /path/to/

# is meaning:

$ codesign -f -s 'iPhone Developer: XXXX (XXXX)' /path/to/

# 'f' is replacing-exist-sign(flag)'s abbr
# 's' is signature(query)'s abbr 

CommandLine is supporting parse multi-abbrs!


IOPaths is a type of value without any key. It’s usually used in input, output path. Such as:

$ cd /change/to/directory/      # inpuut
$ mkdir /create/new/folder      # input
$ zip /to/.zip /source/folder   # output & input

you can execute the code before parse.

CLCommand *zip = [CLCommand main]; // get main command (without any command or subcommands)

    User must type in an output path and one or more input path(s)
    .setExplain(@"output key");
    .setExplain(@"Input path");
    .setExplain(@"Input path");

[zip onHandlerRequest:^CLResponse *(CLCommand *command, CLRequest *request) {
    NSArray *paths = request.paths; // paths.count >= 2
    NSString *output = paths.firstObject;
    NSArray *inputs = ({
        NSMutableArray *inputs = paths.mutableCopy;
        [input removeObjectAtIndex:0];

    NSString *fullOutput = [CLIOPath abslutePath:output]; // replace `~` with $HOME and append current directory if needs.
    //  to zip
    return [CLResponse success:nil];


After you defined all commands and their subcommands, you can parse arguments.

1. Make a request

//  Making with arguments of main()
CLRequest *request = [CLRequest requestWithArgc:argc argv:argv];

//  Making with NSProcressInfo.processInfo.arguments
CLRequest *request = [CLRequest request];

//  See more in CLRequest.h ...

2. Parse request

CLResponse *response = [CLCommand handleRequest:request];

3. Return in main()

if (response.error) {
    return response.error.code;
} else {
    return 0;

Helping Infomations

When should the tool print helping infomation ?

  1. User type in --help or -h for helping
  2. User type in illegal arguments. Such as: inputed 2 paths but 3 required, didnot input required query…

CommandLine will auto create a colorfull helping infomation and print automatically.

Colorfull helping infomation ? Yes ! Just like CocoaPods.

CocoaPods Helping Infomations

Special output

1. Verbose

Print more infomations mode.

It will be triggered by flag --verbose.

You can use in task:

[request verbose:@"Making temp directory: %@", tempDirectory];
//  it will be print if the request contains `verbose` flag.
//  auto append a 'n' in end.

2. Success

Print green text.

You can use in task:

[request success:@"Done! There are %lu devices in the mobileprovision", devices.count];// devices is instance of NSArray
//  print the text render with green color
//  auto append a 'n' in end.

3. Warning

Pring yellow text.

[request warning:@"The directory is not exist, it will be ignore."];
//  print the text render with yellow color
//  auto append a 'n' in end.

4. Error

Print red text.

You can use in task:

[request error:@"Error: %@", error];// error is instance of NSError
//  print the text render with red color
//  auto append a 'n' in end.

5. More Info

Print light text.

You can use in task:

[request info:@"XXXXXX"];
//  print the text with light font.
//  auto append a 'n' in end.


Print version of this tool.

[CLCommand setVersion:@"1.0.0"]; // do once.
$ tool --version

# or

$ tool -v


NSString *fileList = CLLaunch(@"ls", @"-a");
CLLaunchAt(@"~", @"zip", @"-qry", @"", @".");

Print colorful text

#import "CCText.h"
CCPrintf(CCStyleBord|CCStyleItalic, @"A text with %@ and %@", @"bord", @"italic");
// see move CCStyle in CCText.h



Latest podspec

    "name": "CommandLine",
    "version": "0.1.0",
    "summary": "A command line arguments parser of Objective-C",
    "description": "If you want to build a command line tool and require some arguments when running it.nYou can link CommandLine, and dont warry about the arguments-parsing. CommandLinencan complete it for you.",
    "homepage": "",
    "license": "MIT",
    "authors": {
        "u51b7u79cb": "[email protected]"
    "platforms": {
        "osx": "10.10"
    "source": {
        "git": "",
        "tag": "0.1.0"
    "source_files": "CommandLine/**/*.{h,m}",
    "public_header_files": "CommandLine/Public/*.h"

Pin It on Pinterest

Share This