Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

Unparsing skips arguments with values equal to the argument type's default value

Đang mở
#850 5 bình luận 3 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
45/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
csharp
Lĩnh vực
cli

Hướng nghiên cứu

Tái hiện vấn đề với ứng dụng Program và TestArgs được cung cấp, tập trung vào Parser.FormatCommandLine với settings.SkipDefault = true. So sánh đầu ra của nó đối với các giá trị 0, 1 và 2 với các dòng lệnh mong đợi, sau đó xác minh rằng mỗi kết quả đều có thể được phân tích cú pháp theo chiều ngược lại để trở về các giá trị ban đầu.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Using CommandLineParser 2.9.1, the unparsing behaviour with SkipDefault = true seems to be incorrect. Here is a test app to reproduce the behaviour.

using CommandLine;

namespace UnparsingTest
{
    public class Program
    {
        public class TestArgs
        {
            [Option('c', Required = true)]
            public int CompulsoryWithoutDefault { get; set; }

            [Option('d', Required = true, Default = 1)]
            public int CompulsoryWithDefault { get; set; } = 1;

            [Option('o', Required = false, Default = 1)]
            public int Optional { get; set; } = 1;
        }

        static void Main()
        {
            Parser parser = new Parser();

            for (int i = 0; i <= 2; ++i)
            {
                TestArgs args = new TestArgs()
                {
                    CompulsoryWithoutDefault = i,
                    CompulsoryWithDefault = i,
                    Optional = i,
                };

                string commandLine = parser.FormatCommandLine(args, settings => settings.SkipDefault = true);
                System.Console.WriteLine($"CompulsoryWithDefault: {args.CompulsoryWithDefault}, CompulsoryWithoutDefault: {args.CompulsoryWithoutDefault}, Optional: {args.Optional}, Command line: \"{commandLine}\"");
            }
        }
    }
}

There are 2 compulsory arguments, one with a Default value and one without a Default value. CompulsoryWithDefault and Optional have their Default values set to any value that is not default(int).

The test app's console output shows the unparsed command line for the 3 sets of arguments

CompulsoryWithDefault: 0, CompulsoryWithoutDefault: 0, Optional: 0, Command line: ""
CompulsoryWithDefault: 1, CompulsoryWithoutDefault: 1, Optional: 1, Command line: "-c 1"
CompulsoryWithDefault: 2, CompulsoryWithoutDefault: 2, Optional: 2, Command line: "-c 2 -d 2 -o 2"

The unparsed command line for the first case will fail round trip parsing because the compulsory -c and -d arguments are missing and -o is missing so Optional will have a value of 1 instead of 0. The unparsed command line for the second case will also fail round trip parsing because the compulsory -d argument is missing. Only the third case is giving correct output.

The bug appears to be that the unparser is skipping Required = true arguments, arguments with value equal to Default = {value} and arguments with value equal to default(typeof(argument)).

The correct behaviour would be to only skip arguments that do not have Required = true and whose value equals the value specified in Default = {value}.

The expected console output would be

CompulsoryWithDefault: 0, CompulsoryWithoutDefault: 0, Optional: 0, Command line: "-c 0 -d 0 -o 0"
CompulsoryWithDefault: 1, CompulsoryWithoutDefault: 1, Optional: 1, Command line: "-c 1 -d 1"
CompulsoryWithDefault: 2, CompulsoryWithoutDefault: 2, Optional: 2, Command line: "-c 2 -d 2 -o 2"

These command lines would then correctly round trip parse/unparse.

Ngôn ngữ chính
C#
Star
4.8k
Fork
478
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của commandlineparser/commandline

Tất cả issue của commandlineparser/commandline

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.