-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
101 lines (82 loc) · 3.4 KB
/
Copy pathProgram.cs
File metadata and controls
101 lines (82 loc) · 3.4 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System.CommandLine;
using System.CommandLine.Builder;
using System.CommandLine.Invocation;
using TrackTime.Cli;
using TrackTime.Cli.Shared;
using TrackTime.Cli.Stamps.Model;
using TrackTime.Lib.Stamps;
using TrackTime.Lib.Store;
StampService stampService = new();
var rootCommand = new RootCommand("Sample app for System.CommandLine");
var timeArgument = new Argument<CustomDateTimeArgument>(
name: "time",
description: "The time you want to stamp.",
parse: input => new CustomDateTimeArgument(input.Tokens.Single().Value));
var atOption = new Option<TimeOnly?>(
name: "--at",
description: "Specify the time of day if not done yet. Format: H:mm.",
parseArgument: input => input.Tokens.Count > 0 ? TimeOnly.ParseExact(input.Tokens.Single().Value, "H:mm") : null
);
var reasonOption = new Option<string>(
name: "--reason",
description: "Add a description/reason for this stamp");
var inOption = new Option<bool>(
name: "--in",
description: "Specify that this stamp entry should be the start point."
);
var outOption = new Option<bool>(
name: "--out",
description: "Specify that this stamp entry should be the end point."
);
var fileOption = new Option<FileInfo?>(
name: "--file",
description: "The file where the stamps are stored in.",
// isDefault: true,
parseArgument: result =>
{
if (result.Tokens.Count == 0)
{
return new FileInfo("sampleQuotes.txt");
}
string? filePath = result.Tokens.Single().Value;
if (File.Exists(filePath)) return new FileInfo(filePath);
result.ErrorMessage = "File does not exist";
return null;
});
var stampCommand = new Command("stamp", "Stamps the specified time.");
stampCommand.AddArgument(timeArgument);
stampCommand.AddOption(reasonOption);
stampCommand.AddOption(inOption);
stampCommand.AddOption(outOption);
stampCommand.AddOption(atOption);
var listStampsCommand = new Command(name: "list", description: "List all stamps for a specific day");
listStampsCommand.AddAlias("ls");
var todayOption = new Option<bool>(name: "--today");
var timeForListArgument = new Argument<CustomDateTimeArgument?>(
name: "time",
description: "The time you want to stamp.",
parse: input => input.Tokens.Any() ? new CustomDateTimeArgument(input.Tokens.Single().Value) : null);
listStampsCommand.AddArgument(timeForListArgument);
listStampsCommand.SetHandler(async (ListStampsModel listModel, FileInfo? file) =>
{
var stamps = await stampService.GetStamps(listModel.Today ? DateTime.Today : listModel.Day, file);
if (stamps == null)
{
Console.WriteLine("Can't read stamps.");
return;
}
foreach (var stamp in stamps)
{
Console.WriteLine(stamp.ToString());
}
}, new ListStampsModelBinder(todayOption, timeForListArgument), fileOption);
stampCommand.AddCommand(listStampsCommand);
rootCommand.AddCommand(stampCommand);
rootCommand.AddOption(fileOption);
stampCommand.SetHandler(async (CustomDateTimeArgument time, TimeOnly? at, string? reason, FileInfo? file, bool stampIn, bool stampOut) =>
{
await stampService.Stamp(time, at, reason, file, stampIn, stampOut);
},
timeArgument, atOption, reasonOption, fileOption, inOption, outOption);
var parser = new CommandLineBuilder(rootCommand).UseExceptionHandler((e, context) => Console.WriteLine(e.Message), errorExitCode: 1).Build();
return await rootCommand.InvokeAsync(args);