Skip to content

Some cli arg parse problem in MakeParameters and suggestion to use professional cli argparser #106

Open
@hiddenblue

Description

#107

当前项目中的cli 命令行参数解析有一些问题:

  1. 在正常安装之后,如果使用下面这样的命令,将会出现错误,显示path不存在。
  2. 强烈建议使用一个专门的c#命令参数解析库,比如https://github.com/commandlineparser/commandline或者https://github.com/dotnet/command-line-api/tree/main/src 。这样可以避免一些复杂情况的逻辑错误。
chase@chase-HP ~/R/uml> puml-gen ./observer.class -createAssociation
System.ArgumentException: The value cannot be an empty string. (Parameter 'path')
   at System.ArgumentException.ThrowNullOrEmptyException(String argument, String paramName)
   at System.IO.Directory.CreateDirectory(String path)
   at PlantUmlClassDiagramGenerator.Program.GeneratePlantUmlFromFile(Dictionary`2 parameters) in C:\pierre3\PlantUmlClassDiagramGenerator\src\PlantUmlClassDiagramGenerator\Program.cs:line 66

我在检查Generator之后,发现
这部分代码出了问题。

即便-createAssociation作为参数在options中找到了,但是还是会添加到out参数当中。
这个是不合理的,导致了path错误。

            if (options.TryGetValue(arg, out OptionType value))
            {
                if (value == OptionType.Value)
                {
                    currentKey = arg;
                }
                else
                {
                    parameters.Add(arg, string.Empty);
                }
            }

            if(!parameters.TryAdd("in", arg))
            {
                parameters.TryAdd("out", arg);
            }

Current Issues with CLI Command-Line Argument Parsing in the Project:

  1. After a normal installation, using the following command will result in an error, indicating that the path does not exist.
  2. It is strongly recommended to use a dedicated C# command-line argument parsing library, such as https://github.com/commandlineparser/commandline or https://github.com/dotnet/command-line-api/tree/main/src. This can help avoid logic errors in complex scenarios.
chase@chase-HP ~/R/uml> puml-gen ./observer.class -createAssociation
System.ArgumentException: The value cannot be an empty string. (Parameter 'path')
   at System.ArgumentException.ThrowNullOrEmptyException(String argument, String paramName)
   at System.IO.Directory.CreateDirectory(String path)
   at PlantUmlClassDiagramGenerator.Program.GeneratePlantUmlFromFile(Dictionary`2 parameters) in C:\pierre3\PlantUmlClassDiagramGenerator\src\PlantUmlClassDiagramGenerator\Program.cs:line 66

After inspecting the Generator, I found that there is an issue with this part of the code.

Even though -createAssociation is found as an argument in the options, it is still added to the out parameters.
This is unreasonable and leads to the path error.

            if (options.TryGetValue(arg, out OptionType value))
            {
                if (value == OptionType.Value)
                {
                    currentKey = arg;
                }
                else
                {
                    parameters.Add(arg, string.Empty);
                }
            }

            if(!parameters.TryAdd("in", arg))
            {
                parameters.TryAdd("out", arg);
            }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions