Skip to content

Commit

Permalink
Add example showing all use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yakovypg committed May 20, 2024
1 parent 1141172 commit 46e3ef6
Showing 1 changed file with 139 additions and 0 deletions.
139 changes: 139 additions & 0 deletions Examples/NetArgumentParser.Examples.AllUseCases/src/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using NetArgumentParser;
using NetArgumentParser.Converters;
using NetArgumentParser.Generators;
using NetArgumentParser.Options;
using NetArgumentParser.Options.Context;

bool verbose = false;
bool quick = false;
int? angle = default;
TimeSpan? time = default;
FileMode? fileMode = default;
List<string> inputFiles = [];
DateTime? date = default;
string? name = default;

var parser = new ArgumentParser()
{
NumberOfArgumentsToSkip = 0,
RecognizeSeveralCharsInShortNameAsSeveralOptions = true,
RecognizeSlashAsOption = true,
ProgramName = "ProgramName",
ProgramDescription = "What the program does",
ProgramEpilog = "Text at the bottom"
};

var nameOption = new ValueOption<string>("name", "n", afterValueParsingAction: t => name = t)
{
Converter = new ValueConverter<string>(t => t.ToUpper())
};

var options = new ICommonOption[]
{
nameOption,

new HelpOption("help", "h",
description: "show command-line help",
afterHandlingAction: () =>
{
Console.WriteLine(parser.GenerateProgramDescription());
Environment.Exit(0);
}),

new FlagOption("verbose", "v",
description: "be verbose",
afterHandlingAction: () => verbose = true),

new FlagOption(string.Empty, "q",
description: "use fast algorithm",
afterHandlingAction: () => quick = true),

new MultipleValueOption<string>("input", "i",
description: "images that need to be processed",
isRequired: true,
contextCapture: new OneOrMoreContextCapture(),
afterValueParsingAction: t => inputFiles = new List<string>(t)),

new ValueOption<int>("angle", "a",
description: "angle by which you want to rotate the image",
isRequired: true,
afterValueParsingAction: t => angle = t)
};

var additionalOptions = new ICommonOption[]
{
new ValueOption<TimeSpan>("time", "t",
description: "maximum algorithm execution time",
afterValueParsingAction: t => time = t),

new EnumValueOption<FileMode>("file-mode", string.Empty,
description: "specifies how the operatng system should open a file",
defaultValue: new DefaultOptionValue<FileMode>(FileMode.OpenOrCreate),
afterValueParsingAction: t => fileMode = t),

new MultipleValueOption<int>("date",
description: "next date the program update notification will be displayed",
metaVariable: "D",
contextCapture: new FixedContextCapture(3),
afterValueParsingAction: t => date = new DateTime(t[0], t[1], t[2]))
};

var converters = new IValueConverter[]
{
new ValueConverter<TimeSpan>(t =>
{
int[] data = t.Split(',').Select(int.Parse).ToArray();
return data.Length switch
{
1 => new TimeSpan(data[0]),
3 => new TimeSpan(data[0], data[1], data[2]),
4 => new TimeSpan(data[0], data[1], data[2], data[3]),
5 => new TimeSpan(data[0], data[1], data[2], data[3], data[4]),
6 => new TimeSpan(data[0], data[1], data[2], data[3], data[4], data[5]),
_ => throw new FormatException()
};
})
};

var descriptionGenerator = new DescriptionGenerator(parser)
{
OptionExamplePrefix = new string(' ', 2),
OptionExampleCharsLimit = 30,
WindowWidth = Console.WindowWidth
};

parser.DescriptionGenerator = descriptionGenerator;

parser.AddOptions(options);
parser.AddConverters(converters);

OptionGroup<ICommonOption> group = parser.AddOptionGroup("Additional options:");
group.AddOptions(additionalOptions);

List<string> extraArguments = [];

try
{
parser.Parse(args, out extraArguments);
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
return;
}

Console.WriteLine($"Extra arguments: {string.Join(' ', extraArguments)}");
Console.WriteLine($"Verbose: {verbose}");
Console.WriteLine($"Quick: {quick}");
Console.WriteLine($"Angle: {angle}");
Console.WriteLine($"Time: {time}");
Console.WriteLine($"File mode: {fileMode}");
Console.WriteLine($"Input files: {string.Join(' ', inputFiles)}");
Console.WriteLine($"Date: {date?.ToLongDateString()}");
Console.WriteLine($"Name: {name}");

0 comments on commit 46e3ef6

Please sign in to comment.