[1146] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Text;
|
---|
| 4 | using System.Xml.Serialization;
|
---|
| 5 |
|
---|
| 6 | namespace IndianHealthService.BMXNet.WinForm.Configuration
|
---|
| 7 | {
|
---|
| 8 | internal class CommandLineArguments
|
---|
| 9 | {
|
---|
| 10 | //command line, no forward slashes in value /flag /key:value /key:"two part value"
|
---|
| 11 | public Settings ParseSlashKeyValue(String[] arguments, bool ignoreErrors)
|
---|
| 12 | {
|
---|
| 13 | Settings answer = new Settings();
|
---|
| 14 | foreach (String each in arguments)
|
---|
| 15 | {
|
---|
| 16 | String toParse = each;
|
---|
| 17 | try
|
---|
| 18 | {
|
---|
| 19 | if (!each.EndsWith(".exe"))
|
---|
| 20 | {
|
---|
| 21 | if (toParse.StartsWith(@"""") && toParse.EndsWith(@""""))
|
---|
| 22 | {
|
---|
| 23 | toParse = toParse.Substring(1, toParse.Length - 2);
|
---|
| 24 | }
|
---|
| 25 | String[] peices = toParse.Split(new char[] { ':' });
|
---|
| 26 | String key = null;
|
---|
| 27 | String value = null;
|
---|
| 28 | if (peices.Length > 0)
|
---|
| 29 | {
|
---|
| 30 | key = peices[0];
|
---|
| 31 | if (key.StartsWith(@"/"))
|
---|
| 32 | {
|
---|
| 33 | key = key.Substring(1, key.Length - 1);
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | if (peices.Length == 1)
|
---|
| 37 | {
|
---|
| 38 | value = "true";
|
---|
| 39 | }
|
---|
| 40 | else if (peices.Length == 2)
|
---|
| 41 | {
|
---|
| 42 | value = peices[1].Replace(@"""", "");
|
---|
| 43 | }
|
---|
| 44 | else
|
---|
| 45 | {
|
---|
| 46 | key = null;
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | if (key == null)
|
---|
| 51 | {
|
---|
| 52 | throw new Exception(@"Unable to find command key.");
|
---|
| 53 | }
|
---|
| 54 | else
|
---|
| 55 | {
|
---|
| 56 | answer.AddSetting(new Setting() { Name = key.Trim(), Value = value.Trim(), IsPersistent = false });
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 | catch (Exception problem)
|
---|
| 61 | {
|
---|
| 62 | if (!ignoreErrors)
|
---|
| 63 | {
|
---|
| 64 | throw new Exception(@"Unable to parse command-line arguments. Valid format is /flag /key:value /key:""two part value"", no forward slashes allowed", problem);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | return answer;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|