Created
September 20, 2018 04:26
-
-
Save danielplawgo/72dccd8539097f5ac95d5e4ed6538b0a to your computer and use it in GitHub Desktop.
Obsługa parametrów w aplikacji konsolowej za pomocą CommandLineParser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.exe param1 param2 param3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.exe -f result.json -l log.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.exe --file result.json --log log.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IVerb | |
{ | |
void Run(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Options | |
{ | |
[Option('f', "file", HelpText = "The result file name.", Required = true)] | |
public string ResultFile { get; set; } | |
[Option('s', "serializer", HelpText = "The result file serializer.", Required = false, Default = "json")] | |
public string Serializer { get; set; } | |
[Value(0, HelpText = "Input files names.", Required = true)] | |
public IEnumerable<string> InputFiles { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Options { | |
[Value(0)] | |
public int IntValue { get; set; } | |
[Value(1, Min=1, Max=3)] | |
public IEnumerable<string> StringSeq { get; set; } | |
[Value(2)] | |
public double DoubleValue { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Verb("pack", HelpText = "Pack input files.")] | |
public class PackOptions : Options | |
{ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Verb("pack", HelpText = "Pack input files.")] | |
public class PackOptions : Options, IVerb | |
{ | |
public void Run() | |
{ | |
Console.WriteLine($"Parsed {GetType().Name}:"); | |
Console.WriteLine(ToString()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
// parsing parameters from args | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var result = Parser.Default.ParseArguments<Options>(args); | |
result.WithParsed(r => Run(r)); | |
} | |
private static void Run(Options options) | |
{ | |
Console.WriteLine($"Parsed {options.GetType().Name}:"); | |
Console.WriteLine(options.ToString()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var result = Parser.Default.ParseArguments<PackOptions, TestOptions>(args); | |
result | |
.WithParsed<PackOptions>(r => Run(r)) | |
.WithParsed<TestOptions>(r => Run(r)); | |
} | |
private static void Run(Options options) | |
{ | |
Console.WriteLine($"Parsed {options.GetType().Name}:"); | |
Console.WriteLine(options.ToString()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var iverb = typeof(IVerb); | |
var verbs = typeof(Program).Assembly.GetTypes().Where(t => iverb.IsAssignableFrom(t)); | |
var result = Parser.Default.ParseArguments(args, verbs.ToArray()); | |
result | |
.WithParsed<IVerb>(r => r.Run()); | |
} | |
private static void Run(Options options) | |
{ | |
Console.WriteLine($"Parsed {options.GetType().Name}:"); | |
Console.WriteLine(options.ToString()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Verb("test", HelpText = "Test packing input files.")] | |
public class TestOptions : Options | |
{ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Verb("test", HelpText = "Test packing input files.")] | |
public class TestOptions : Options, IVerb | |
{ | |
public void Run() | |
{ | |
Console.WriteLine($"Parsed {GetType().Name}:"); | |
Console.WriteLine(ToString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment