macOS 开发 - BRLOptionParser


一、引言

进入看到 Python 中有使用 optparse
回想起 OC 中使用的的 BRLOptionParser


optparse 学习地址:https://blog.csdn.net/eleanoryss/article/details/70213888

BRLOptionParser 下载地址
https://github.com/stephencelis/BRLOptionParser


二、BRLOptionParser 简介

BRLOptionParser 可放在可执行文件中,创建命令格式,接收命令并作出相应。


三、使用方法

1、官方方法

Install With CocoaPods:

# Podfile
pod 'BRLOptionParser', '~> 0.3.1'
Example

// main.m

#import <BRLOptionParser/BRLOptionParser.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSString *name = @"world";
        BOOL verbose = NO;

        BRLOptionParser *options = [BRLOptionParser new];

        [options setBanner:@"usage: %s [-n <name>] [-vh]", argv[0]];
        [options addOption:"name" flag:'n' description:@"Your name" argument:&name];
        [options addSeparator];
        [options addOption:"verbose" flag:'v' description:nil value:&verbose];
        __weak typeof(options) weakOptions = options;
        [options addOption:"help" flag:'h' description:@"Show this message" block:^{
            printf("%s", [[weakOptions description] UTF8String]);
            exit(EXIT_SUCCESS);
        }];

        NSError *error = nil;
        if (![options parseArgc:argc argv:argv error:&error]) {
            const char * message = error.localizedDescription.UTF8String;
            fprintf(stderr, "%s: %s\n", argv[0], message);
            exit(EXIT_FAILURE);
        }

        if (verbose) {
            fprintf(stderr, "(Preparing to say hello...)\n");
        }

        printf("Hello, %s!\n", name.UTF8String);
    }

    return EXIT_SUCCESS;
}

In practice:

$ hello
Hello, world!
$ hello -h
usage: hello [-n <name>] [-vh]
    -n, --name                       Your name

    -v, --verbose
    -h, --help                       Show this message
$ hello -n
hello: option `-n' requires an argument
$ hello --name Stephen
Hello, Stephen!
$ hello -vngoodbye
(Preparing to say hello...)
Hello, goodbye!
$ hello --goodbye
hello: unrecognized option `--goodbye'

2、用可执行程序执行

2.1 创建可执行程序

如在项目中创建可执行程序,并用可执行程序来调用。
如何创建可执行程序:https://blog.csdn.net/lovechris00/article/details/80377406


2.2 编写可执行程序的 main.m

#import <Foundation/Foundation.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <BRLOptionParser/BRLOptionParser.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...

//        NSLog(@"");
        NSLog(@"\n commandHelper Hello, World!");

        BRLOptionParser *options = [BRLOptionParser new];
        [options setBanner:@"Usage: %s [-v] [-m auto|global|off] [-u <url>] [-p <port>] [-r <port>]", argv[0]];

        // Version
        [options addOption:"version" flag:'v' description:@"Print the version number." block:^{

            printf("current version is 1.0.0.2");
            exit(EXIT_SUCCESS);
        }];

        // Help
        __weak typeof(options) weakOptions = options;
        [options addOption:"help" flag:'h' description:@"Show this message" block:^{
            printf("%s", [[weakOptions description] UTF8String]);
            exit(EXIT_SUCCESS);
        }];

        //使上述方法生效
        NSError *error = nil;
        if (![options parseArgc:argc argv:argv error:&error]) {
            const char * message = error.localizedDescription.UTF8String;
            fprintf(stderr, "%s: %s\n", argv[0], message);
            NSLog(@"-- 失败 ");
            exit(EXIT_FAILURE);
        }
}

2.3 主程序调用

- (void)getAuthHelper{

    NSString *helperToolPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"commandHelper"] ;
    NSLog(@"path = %@",helperToolPath);
    ///path = Users/administrator/Library/Developer/Xcode/DerivedData/Mac_Uninstall-fgneqbuazqhimnerzzdgqmxysxnl/Build/Products/Debug/Mac_Uninstall.app/Contents/Resources/commandHelper

    NSArray *args = [NSArray arrayWithObjects:helperToolPath,@"-v", nil];
    [NSTask launchedTaskWithLaunchPath:helperToolPath arguments:args];

}

打印结果

 path = /Users/administrator/Library/Developer/Xcode/DerivedData/Mac_Uninstall-fgneqbuazqhimnerzzdgqmxysxnl/Build/Products/Debug/MacTools.app/Contents/Resources/commandHelper
2018-06-13 20:28:51.658305+0800 AuthTools[36167:783365] 
 commandHelper Hello, World!
current version is 1.0.0.2

猜你喜欢

转载自blog.csdn.net/lovechris00/article/details/80684801