|
1 | | -using System; |
2 | | -using System.Collections; |
3 | | -using System.Collections.Generic; |
4 | 1 | using System.CommandLine; |
5 | | -using System.IO; |
6 | | -using System.Linq; |
7 | 2 |
|
| 3 | +var inOpt = new Option<string>("--input-file") |
| 4 | +{ |
| 5 | + Description = "Input markdown file(s) path", |
| 6 | + Required = true |
| 7 | +}; |
| 8 | +var outOpt = new Option<string>("--output-file") |
| 9 | +{ |
| 10 | + Description = "Output TOC file or directory path", |
| 11 | + DefaultValueFactory = _ => "./" |
| 12 | +}; |
| 13 | +var tocOpt = new Option<string?>("--toc-file") |
| 14 | +{ |
| 15 | + Description = "TOC file to merge" |
| 16 | +}; |
8 | 17 |
|
9 | | -class Program |
| 18 | +var rootCommand = new RootCommand("Simple tool to generate table-of-content yaml files from Markdown files") |
10 | 19 | { |
11 | | - static async Task<int> Main(string[] args) |
12 | | - { |
13 | | - var inArg = new Argument<string>("--input-file"); |
14 | | - inArg.Description = "Input markdown file(s) path"; |
15 | | - var outArg = new Argument<string>("--output-file"); |
16 | | - outArg.DefaultValueFactory = (r) => "./"; |
17 | | - outArg.Description= "Output TOC file or directory path"; |
18 | | - var toc = new Option<string>("--toc-file"); |
19 | | - toc.Description="TOC file to merge"; |
20 | | - var rootCommand = new RootCommand(); |
21 | | - rootCommand.Description = "Simple tool to generate table-of-content yaml files from Markdown files"; |
22 | | - |
23 | | - rootCommand.Add(inArg); |
24 | | - rootCommand.Add(outArg); |
25 | | - rootCommand.Add(toc); |
26 | | - rootCommand.SetAction(p => Md2TocGenerator.Generate(p.GetValue(inArg), p.GetValue(outArg), p.GetValue(toc))); |
| 20 | + inOpt, |
| 21 | + outOpt, |
| 22 | + tocOpt |
| 23 | +}; |
27 | 24 |
|
28 | | - // Parse the incoming args and invoke the handler |
29 | | - //return await rootCommand.InvokeAsync(args); |
30 | | - throw new ArgumentException("WTF"); |
31 | | - } |
| 25 | +rootCommand.SetAction(async (parseResult, _) => |
| 26 | +{ |
| 27 | + await Md2TocGenerator.Generate( |
| 28 | + parseResult.GetValue(inOpt)!, |
| 29 | + parseResult.GetValue(outOpt)!, |
| 30 | + parseResult.GetValue(tocOpt) ?? string.Empty); |
| 31 | + return 0; |
| 32 | +}); |
32 | 33 |
|
33 | | - |
34 | | -} |
| 34 | +return await rootCommand.Parse(args).InvokeAsync(); |
0 commit comments