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

Commit

Permalink
feat: use NDesk Options parse args
Browse files Browse the repository at this point in the history
  • Loading branch information
CSUwangj committed Oct 8, 2019
1 parent 93a8068 commit 23c9e48
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
65 changes: 46 additions & 19 deletions md2docx/md2docx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,23 @@
using Pic = DocumentFormat.OpenXml.Drawing.Pictures;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using NDesk.Options;

namespace md2docx
{
class Md2Docx
{
static Dictionary<string, string> info = new Dictionary<string, string>();
static string mdPath = "input.md";
static string filePath = "";
static string configPath = "config.json";

/// <summary>
/// Print usage without exit
/// </summary>
private static void Usage()
private static void Usage(OptionSet p)
{
Console.WriteLine(@"Usage: ./md2docx {markdown} {config_json} {output}
default parameter will be
markdown -> input.md
config_json -> config.json
output -> <id><name><filename>.docx");
Console.WriteLine(@"Usage: md2docx [OPTIONS]
Convert markdown to docx with specified options.
Opntions:");
p.WriteOptionDescriptions(Console.Out);
}

/// <summary>
Expand All @@ -38,19 +35,49 @@ private static void Usage()
/// <param name="args">{markdown} {config_json} {output}</param>
private static void Main(string[] args)
{
Usage();
if (args.Length > 0)
string docPath = "";
string mdPath = "input.md";
string configPath = "config.json";
bool showHelp = false;
int useDefault = 3;
bool quiet = false;
var p = new OptionSet
{
mdPath = args[0];
{ "i|input=", "the {INPUT} markdown file path. Default value is input.md.",
v => { mdPath = v; useDefault -= 1; } },
{ "o|output=", "the {OUTPUT} docx file path. Default value is <id><name><filename>.docx.",
v => { docPath = v; useDefault -= 1; } },
{ "c|config=", "the {CONFIG} json path. Default value is config.json.",
v => { configPath = v; useDefault -= 1; } },
{ "h|help", "show this message and exit",
v => showHelp = v != null },
{ "q|quiet", "ignore missing args message",
v => quiet = v != null }
};
try
{
p.Parse(args);
}
if (args.Length > 1)
catch (OptionException e)
{
configPath = args[1];
Console.Write("md2docx: ");
Console.WriteLine(e.Message);
Console.WriteLine("Try`md2docx --help' for more information.");
return;
}
if (args.Length > 2)

if (showHelp)
{
filePath = args[2];
Usage(p);
return;
}

if (useDefault != 0 && !quiet)
{
Console.WriteLine("Some arguments are missing, will use default value for you.");
Console.WriteLine("Try`md2docx --help' for more information or use -q stop this message.");
}

string md = System.IO.File.ReadAllText(mdPath);
JObject config = JObject.Parse(System.IO.File.ReadAllText(configPath));
MarkdownDocument mddoc = new MarkdownDocument();
Expand All @@ -62,13 +89,13 @@ private static void Main(string[] args)
{
info = yaml.Children;
}
if (filePath == "")
if (docPath == "")
{
filePath = info["id"] + info["name"] + info["filename"] + ".docx";
docPath = info["id"] + info["name"] + info["filename"] + ".docx";
}
}

using (WordprocessingDocument document = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document))
using (WordprocessingDocument document = WordprocessingDocument.Create(docPath, WordprocessingDocumentType.Document))
{
MainDocumentPart mainDocumentPart1 = document.AddMainDocumentPart();
GenerateMainDocumentPart1Content(mainDocumentPart1, mddoc, (JObject)config["对应关系"], (JObject)config["可选部分"]);
Expand Down
3 changes: 3 additions & 0 deletions md2docx/md2docx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
<Reference Include="Microsoft.Toolkit.Parsers, Version=5.1.0.0, Culture=neutral, PublicKeyToken=4aff67a105548ee2, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Toolkit.Parsers.5.1.1\lib\netstandard2.0\Microsoft.Toolkit.Parsers.dll</HintPath>
</Reference>
<Reference Include="NDesk.Options, Version=0.2.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\NDesk.Options.0.2.1\lib\NDesk.Options.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
Expand Down
1 change: 1 addition & 0 deletions md2docx/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<package id="DocumentFormat.OpenXml" version="2.9.1" targetFramework="net461" />
<package id="Microsoft.Toolkit" version="5.1.1" targetFramework="net461" />
<package id="Microsoft.Toolkit.Parsers" version="5.1.1" targetFramework="net461" />
<package id="NDesk.Options" version="0.2.1" targetFramework="net461" />
<package id="Newtonsoft.Json" version="12.0.2" targetFramework="net461" />
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net461" />
<package id="System.IO.Packaging" version="4.5.0" targetFramework="net461" />
Expand Down

0 comments on commit 23c9e48

Please sign in to comment.