Get the command parameters in Win app and Console app

本文介绍了一种在Windows和控制台应用程序中解析命令行参数的方法。通过示例代码展示了如何逐个提取并处理命令行参数,适用于多种应用场景。

Usually one exe command is followed by some parameters, such as c:/dir /a; our application can process those parameters, both in win app and console app.

Indicate that all paremeters are Separated by space ' ';

 

// win mode:

void ParseCommandLineArgs()

{

    CString strCmdLine = GetCommandLine();

 

    if (!strCmdLine.IsEmpty())

    {

       // unify the letters

       strCmdLine.MakeUpper();

 

       int nPos = 0;

       do {

           nPos = strCmdLine.Find(TCHAR(' '));

           if (nPos>0)

           {

              // remove the first parameter, it's usually the exe file path

              strCmdLine.Delete( 0, nPos+1);

              CString strCurrent = strCmdLine;

              int nNextPos = strCmdLine.Find(TCHAR(' '));

              // store the current parameter in strCurrent

              if (nNextPos > 0)

                  strCurrent = strCmdLine.Left( nNextPos );

                  // process the parameter

           }

       } while( nPos != -1);

    }

}

 

console mode: (from open SSL Evp_text.c)

Indicate that all paremeters are Separated by space ':';

Its also an easy script debugger.

// the delim is a stop character.

// return the first part,

// and terminate the string.

static char *sstrsep(char **string, const char *delim)

{

    char isdelim[256]; // stop character table

    char *token = *string;

 

    if (**string == 0)

       return NULL;

 

    memset(isdelim, 0, 256);

    isdelim[0] = 1;   // /0 is a end of string

 

    // initial stop character table

    while (*delim)

    {

       isdelim[(unsigned char)(*delim)] = 1;

       delim++;

    }

 

    // begin check

    while (!isdelim[(unsigned char)(**string)])

    {

       (*string)++;

    }

 

    // end the string by /0

    if (**string)

    {

       **string = 0;

       (*string)++;

    }

 

    return token;

}

// remove the warn information

static unsigned char *ustrsep(char **p,const char *sep)

{ return (unsigned char *)sstrsep(p,sep); }

 

int _tmain(int argc, _TCHAR* argv[])

{

    const char *szPara = argv[1];

    for (int i = 0; i < argc; ++i) {

 

       printf("para%d: %s /n",i, argv[i]);

    }

 

// some examples easy explement of test script

//# CBC-AES128.Encrypt and CBC-AES128.Decrypt

//    AES-128-CBC:2B7E151628AED2A6ABF7158809CF4F3C:000102030405060708090A0B0C0D0E0F:6BC1BEE22E409F96E93D7E117393172A:7649ABAC8119B246CEE98E9B12E9197D

 

    FILE *f=fopen(szPara,"r");

    if(!f)

       return 0;

    // in a dead loop, we read every line of script

    for (;;) {

 

       char line[4096];

       char *p;

       char *cipher;

       unsigned char *iv,*key,*plaintext,*ciphertext;

       int encdec; // operation flag

       int kn,in,pn,cn;

 

       if(!fgets((char *)line,sizeof line,f))

           break;

       if(line[0] == '#' || line[0] == '/n')

           continue;

       p=line;

       cipher=sstrsep(&p,":"); 

       key=ustrsep(&p,":");

       iv=ustrsep(&p,":");

       plaintext=ustrsep(&p,":");

       ciphertext=ustrsep(&p,":");

       if (p[-1] == '/n') {

           p[-1] = '/0';

           encdec = -1;

       } else {

           encdec = atoi(sstrsep(&p,"/n"));

       }

    }

    fclose(f);

    return 0;

}

PS C:\Users\forgi\AndroidStudioProjects\ncnn-android-yolov8\ncnn-android-yolov8> ./gradlew cleanBuildCache Welcome to Gradle 7.6! Here are the highlights of this release: - Added support for Java 19. - Introduced `--rerun` flag for individual task rerun. - Improved dependency block for test suites to be strongly typed. - Added a pluggable system for Java toolchains provisioning. For more details see https://docs.gradle.org/7.6/release-notes.html Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details > Configure project :app C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. FAILURE: Build failed with an exception. * What went wrong: Task 'cleanBuildCache' not found in root project 'ncnn-android-yolov8' and its subprojects. * Try: > Run gradlew tasks to get a list of available tasks. > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. See https://docs.gradle.org/7.6/userguide/command_line_interface.html#sec:command_line_warnings BUILD FAILED in 7s PS C:\Users\forgi\AndroidStudioProjects\ncnn-android-yolov8\ncnn-android-yolov8> ./gradlew --recompile-scripts Unknown command-line option '--recompile-scripts'. USAGE: gradlew [option...] [task...] -?, -h, --help Shows this help message. -a, --no-rebuild Do not rebuild project dependencies. -b, --build-file Specify the build file. [deprecated] --build-cache Enables the Gradle build cache. Gradle will try to reuse outputs from previous builds. -c, --settings-file Specify the settings file. [deprecated] --configuration-cache Enables the configuration cache. Gradle will try to reuse the build configuration from previous builds. [incubating] --configuration-cache-problems Configures how the configuration cache handles problems (fail or warn). Defaults to fail. [incubating] --configure-on-demand Configure necessary projects only. Gradle will attempt to reduce configuration time for large multi-project builds. [incubating] --console Specifies which type of console output to generate. Values are 'plain', 'auto' (default), 'rich' or 'verbose'. --continue Continue task execution after a task failure. -D, --system-prop Set system property of the JVM (e.g. -Dmyprop=myvalue). -d, --debug Log in debug mode (includes normal stacktrace). --daemon Uses the Gradle daemon to run the build. Starts the daemon if not running. --export-keys Exports the public keys used for dependency verification. -F, --dependency-verification Configures the dependency verification mode. Values are 'strict', 'lenient' or 'off'. --foreground Starts the Gradle daemon in the foreground. -g, --gradle-user-home Specifies the Gradle user home directory. Defaults to ~/.gradle -I, --init-script Specify an initialization script. -i, --info Set log level to info. --include-build Include the specified build in the composite. -M, --write-verification-metadata Generates checksums for dependencies used in the project (comma-separated list) -m, --dry-run Run the builds with all task actions disabled. --max-workers Configure the number of concurrent workers Gradle is allowed to use. --no-build-cache Disables the Gradle build cache. --no-configuration-cache Disables the configuration cache. [incubating] --no-configure-on-demand Disables the use of configuration on demand. [incubating] --no-daemon Do not use the Gradle daemon to run the build. Useful occasionally if you have configured Gradle to always run with the daemon by default. --no-parallel Disables parallel execution to build projects. --no-scan Disables the creation of a build scan. For more information about build scans, please visit https://gradle.com/build-scans. --no-watch-fs Disables watching the file system. --offline Execute the build without accessing network resources. -P, --project-prop Set project property for the build script (e.g. -Pmyprop=myvalue). -p, --project-dir Specifies the start directory for Gradle. Defaults to current directory. --parallel Build projects in parallel. Gradle will attempt to determine the optimal number of executor threads to use. --priority Specifies the scheduling priority for the Gradle daemon and all processes launched by it. Values are 'normal' (default) or 'low' --profile Profile build execution time and generates a report in the <build_dir>/reports/profile directory. --project-cache-dir Specify the project-specific cache directory. Defaults to .gradle in the root project directory. -q, --quiet Log errors only. --refresh-dependencies Refresh the state of dependencies. --refresh-keys Refresh the public keys used for dependency verification. --rerun-tasks Ignore previously cached task results. -S, --full-stacktrace Print out the full (very verbose) stacktrace for all exceptions. -s, --stacktrace Print out the stacktrace for all exceptions. --scan Creates a build scan. Gradle will emit a warning if the build scan plugin has not been applied. (https://gradle.com/build-scans) --status Shows status of running and recently stopped Gradle daemon(s). --stop Stops the Gradle daemon if it is running. -t, --continuous Enables continuous build. Gradle does not exit and will re-execute tasks when task file inputs change. --update-locks Perform a partial update of the dependency lock, letting passed in module notations change version. [incubating] -V, --show-version Print version info and continue. -v, --version Print version info and exit. -w, --warn Set log level to warn. --warning-mode Specifies which mode of warnings to generate. Values are 'all', 'fail', 'summary'(default) or 'none' --watch-fs Enables watching the file system for changes, allowing data about the file system to be re-used for the next build. --write-locks Persists dependency resolution for locked configurations, ignoring existing locking information if it exists -x, --exclude-task Specify a task to be excluded from execution. -- Signals the end of built-in options. Gradle parses subsequent parameters as only tasks or task options.
08-02
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值