Repository navigation
Expand file tree
/
Copy pathProgramArguments.cs
More file actions
44 lines (40 loc) · 2.07 KB
/
Copy pathProgramArguments.cs
File metadata and controls
44 lines (40 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using Ookii.CommandLine;
using Ookii.CommandLine.Validation;
using System.ComponentModel;
using System.Net;
namespace ArgumentDependencies;
// This sample shows how you can have arguments that require or prohibit usage combined with
// other aruments. In this sample, "-Path" and "-Ip" are mutually exclusive, and "-Port" can
// only be used in combination with "-Ip".
//
// Because "-Path" and "-Ip" can't be used together, we can't make either of them required.
// Doing so would make it impossible to use the other argument. Instead, we use the RequiresAny
// validator on the class to specify that a valid invocation requires either "-Path" or "-Ip".
//
// This sample uses nameof() to refer to the arguments. This is a good idea because there's
// compile-time checks if you change a name, but take note: these attributes all require the
// argument name, *not* the member name, so nameof() only works if they're the same.
//
// If you use a NameTransform that changes the argument names, or use any explicit argument
// names, you CANNOT use nameof()!
[GeneratedParser]
[ApplicationFriendlyName("Ookii.CommandLine Dependency Sample")]
[Description("Sample command line application with argument dependencies. The application parses the command line and prints the results, but otherwise does nothing and none of the arguments are actually used for anything.")]
[RequiresAny(nameof(Path), nameof(Ip))]
partial class ProgramArguments
{
[CommandLineArgument(IsPositional = true)]
[Description("The path to use.")]
public FileInfo? Path { get; set; }
// This argument uses the ProhibitsAttribute to indicate it's mutually exclusive with "-Path".
[CommandLineArgument]
[Description("The IP address to connect to.")]
[Prohibits(nameof(Path))]
public IPAddress? Ip { get; set; }
// This argument uses the RequiresAttribute to indicate it can only be used if "-Ip" is also
// specified.
[CommandLineArgument]
[Description("The port to connect to.")]
[Requires(nameof(Ip))]
public int Port { get; set; } = 80;
}