commandlineparser / commandline

The best C# command line parser that brings standardized *nix getopt style, for .NET. Includes F# support
MIT License
4.46k stars 473 forks source link

Does Command Line Parser affect the program execution? #911

Closed arttsai closed 5 months ago

arttsai commented 5 months ago

My small program stops execution in a for loop without any error. At first glance I thought there might be a problem in dotnet runtime. But after rewriting the program without using command line parser, the program behave normally.

async Task<(int lines, int words, int chars)> CountDirectoryAsync(Option option, string dirPath)
{
    var (lines, words, chars) = (0, 0, 0);

    var files = Directory.GetFiles(dirPath, "*.*", SearchOption.AllDirectories);

    for (var i = 0; i < files.Length; i++)   // this for loop stops after 1 or 2 loops, though more files waiting to process.
    {
        var file = files[i];
        var ext = Path.GetExtension(file).TrimStart('.');
        if (!option.Types!.Contains(ext)) continue;

        var (fileLines, fileWrods, fileChars) = await CountFileAsync(option, file);
        lines += fileLines;
        words += fileWrods;
        chars += fileChars;
    }

    return (lines, words, chars);   
}

This is weird. I don't think a package could affect the execution. This small project only use a package: command line parser. So I think I should report this scenario.

Describe the bug I don't know if this is a bug from command line parser. But in my code, the for loop stop execution without error nor exception. At the first glance I thought the compiler might has some problem. But another branch which doesn't use command line parser can execute without any problem.

To Reproduce This is a small project.

Please see the repository (master branch) https://github.com/arttsai/Mywc. When executing the program, in CountDirectoryAsync(), line 48 (for loop). this for loop might only run 1 or 2 times and then the program terminates without any error.

With branch PureWc, which doesn't use command line parser, the program executes perfectly.

When executing either branch, you need to give command line arguments.

  1. in master branch, arguments: -t cs java -lwc
  2. in PureWc branch, arguments:

Expected behavior Find out the reason why the for loop stops. Find out if command line parser affect the program execution.

Screenshots None.

Additional context None.

arttsai commented 5 months ago

Sorry, I found the reason. I forgot to use 'await'

original:

var result = Parser.Default.ParseArguments<Option>(args)
    .WithParsedAsync(RunAsync);

change to:

var result = await Parser.Default.ParseArguments<Option>(args)
    .WithParsedAsync(RunAsync);

will fix the problem.

I'll close this issue.