Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hiddenblue opened this issue Oct 20, 2024 · 0 comments

Comments

@hiddenblue
Copy link
Contributor

hiddenblue commented Oct 20, 2024

#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);
            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant