Skip to content
This repository was archived by the owner on May 21, 2022. It is now read-only.

Commit 23c9e48

Browse files
committed
feat: use NDesk Options parse args
1 parent 93a8068 commit 23c9e48

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

md2docx/md2docx.cs

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,23 @@
1010
using Pic = DocumentFormat.OpenXml.Drawing.Pictures;
1111
using Newtonsoft.Json.Linq;
1212
using System.Collections.Generic;
13+
using NDesk.Options;
1314

1415
namespace md2docx
1516
{
1617
class Md2Docx
1718
{
1819
static Dictionary<string, string> info = new Dictionary<string, string>();
19-
static string mdPath = "input.md";
20-
static string filePath = "";
21-
static string configPath = "config.json";
2220

2321
/// <summary>
2422
/// Print usage without exit
2523
/// </summary>
26-
private static void Usage()
24+
private static void Usage(OptionSet p)
2725
{
28-
Console.WriteLine(@"Usage: ./md2docx {markdown} {config_json} {output}
29-
default parameter will be
30-
markdown -> input.md
31-
config_json -> config.json
32-
output -> <id><name><filename>.docx");
26+
Console.WriteLine(@"Usage: md2docx [OPTIONS]
27+
Convert markdown to docx with specified options.
28+
Opntions:");
29+
p.WriteOptionDescriptions(Console.Out);
3330
}
3431

3532
/// <summary>
@@ -38,19 +35,49 @@ private static void Usage()
3835
/// <param name="args">{markdown} {config_json} {output}</param>
3936
private static void Main(string[] args)
4037
{
41-
Usage();
42-
if (args.Length > 0)
38+
string docPath = "";
39+
string mdPath = "input.md";
40+
string configPath = "config.json";
41+
bool showHelp = false;
42+
int useDefault = 3;
43+
bool quiet = false;
44+
var p = new OptionSet
4345
{
44-
mdPath = args[0];
46+
{ "i|input=", "the {INPUT} markdown file path. Default value is input.md.",
47+
v => { mdPath = v; useDefault -= 1; } },
48+
{ "o|output=", "the {OUTPUT} docx file path. Default value is <id><name><filename>.docx.",
49+
v => { docPath = v; useDefault -= 1; } },
50+
{ "c|config=", "the {CONFIG} json path. Default value is config.json.",
51+
v => { configPath = v; useDefault -= 1; } },
52+
{ "h|help", "show this message and exit",
53+
v => showHelp = v != null },
54+
{ "q|quiet", "ignore missing args message",
55+
v => quiet = v != null }
56+
};
57+
try
58+
{
59+
p.Parse(args);
4560
}
46-
if (args.Length > 1)
61+
catch (OptionException e)
4762
{
48-
configPath = args[1];
63+
Console.Write("md2docx: ");
64+
Console.WriteLine(e.Message);
65+
Console.WriteLine("Try`md2docx --help' for more information.");
66+
return;
4967
}
50-
if (args.Length > 2)
68+
69+
if (showHelp)
5170
{
52-
filePath = args[2];
71+
Usage(p);
72+
return;
5373
}
74+
75+
if (useDefault != 0 && !quiet)
76+
{
77+
Console.WriteLine("Some arguments are missing, will use default value for you.");
78+
Console.WriteLine("Try`md2docx --help' for more information or use -q stop this message.");
79+
}
80+
5481
string md = System.IO.File.ReadAllText(mdPath);
5582
JObject config = JObject.Parse(System.IO.File.ReadAllText(configPath));
5683
MarkdownDocument mddoc = new MarkdownDocument();
@@ -62,13 +89,13 @@ private static void Main(string[] args)
6289
{
6390
info = yaml.Children;
6491
}
65-
if (filePath == "")
92+
if (docPath == "")
6693
{
67-
filePath = info["id"] + info["name"] + info["filename"] + ".docx";
94+
docPath = info["id"] + info["name"] + info["filename"] + ".docx";
6895
}
6996
}
7097

71-
using (WordprocessingDocument document = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document))
98+
using (WordprocessingDocument document = WordprocessingDocument.Create(docPath, WordprocessingDocumentType.Document))
7299
{
73100
MainDocumentPart mainDocumentPart1 = document.AddMainDocumentPart();
74101
GenerateMainDocumentPart1Content(mainDocumentPart1, mddoc, (JObject)config["对应关系"], (JObject)config["可选部分"]);

md2docx/md2docx.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666
<Reference Include="Microsoft.Toolkit.Parsers, Version=5.1.0.0, Culture=neutral, PublicKeyToken=4aff67a105548ee2, processorArchitecture=MSIL">
6767
<HintPath>..\packages\Microsoft.Toolkit.Parsers.5.1.1\lib\netstandard2.0\Microsoft.Toolkit.Parsers.dll</HintPath>
6868
</Reference>
69+
<Reference Include="NDesk.Options, Version=0.2.1.0, Culture=neutral, processorArchitecture=MSIL">
70+
<HintPath>..\packages\NDesk.Options.0.2.1\lib\NDesk.Options.dll</HintPath>
71+
</Reference>
6972
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
7073
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
7174
</Reference>

md2docx/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<package id="DocumentFormat.OpenXml" version="2.9.1" targetFramework="net461" />
44
<package id="Microsoft.Toolkit" version="5.1.1" targetFramework="net461" />
55
<package id="Microsoft.Toolkit.Parsers" version="5.1.1" targetFramework="net461" />
6+
<package id="NDesk.Options" version="0.2.1" targetFramework="net461" />
67
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net461" />
78
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net461" />
89
<package id="System.IO.Packaging" version="4.5.0" targetFramework="net461" />

0 commit comments

Comments
 (0)