1010using Pic = DocumentFormat . OpenXml . Drawing . Pictures ;
1111using Newtonsoft . Json . Linq ;
1212using System . Collections . Generic ;
13+ using NDesk . Options ;
1314
1415namespace 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 [ "可选部分" ] ) ;
0 commit comments