public static ChoCommandLineParserSettings RefreshSection() { lock (_padLock) { _instance = null; ConfigurationManager.RefreshSection(SECTION_NAME); } return(Me); }
internal bool GetShowUsageIfEmpty() { ChoCommandLineParserSettings commandLineParserSettings = ChoCommandLineParserSettings.Me; bool showUsageIfEmpty = false; if (ShowUsageIfEmptyInternal != null) { showUsageIfEmpty = ShowUsageIfEmptyInternal.Value; } else { showUsageIfEmpty = commandLineParserSettings.ShowUsageIfEmpty; } return(showUsageIfEmpty); }
internal bool GetDisplayDefaultValue() { ChoCommandLineParserSettings commandLineParserSettings = ChoCommandLineParserSettings.Me; bool displayDefaultValue = false; if (DisplayDefaultValueInternal != null) { displayDefaultValue = DisplayDefaultValueInternal.Value; } else { displayDefaultValue = commandLineParserSettings.DisplayDefaultValue; } return(displayDefaultValue); }
public ChoCommandLineArgParser(ChoCommandLineParserSettings commandLineParserSettings) { if (commandLineParserSettings == null) { throw new ArgumentNullException("commandLineParserSettings"); } _commandLineParserSettings = commandLineParserSettings; if (_commandLineParserSettings.IgnoreCase) { _argsDict = new Dictionary <string, string>(StringComparer.CurrentCultureIgnoreCase); } else { _argsDict = new Dictionary <string, string>(); } }
public virtual string GetUsage() { Type type = GetType(); StringBuilder builder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); ChoCommandLineParserSettings commandLineParserSettings = ChoCommandLineParserSettings.Me; char cmdLineValueSeparator = commandLineParserSettings.ValueSeparators != null && commandLineParserSettings.ValueSeparators.Length > 0 ? commandLineParserSettings.ValueSeparators[0] : ':'; char cmdLineSwitchChar = commandLineParserSettings.SwitchChars != null && commandLineParserSettings.SwitchChars.Length > 0 ? commandLineParserSettings.SwitchChars[0] : '-'; builder.Append(Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location).ToUpper()); MemberInfo[] memberInfos = ChoTypeMembersCache.GetAllMemberInfos(type); if (memberInfos != null && memberInfos.Length > 0) { ChoCommandLineArgAttribute commandLineArgumentAttribute = null; ChoDefaultCommandLineArgAttribute defaultCommandLineArgAttribute = null; ChoPositionalCommandLineArgAttribute posCommandLineArgAttribute = null; List <Tuple <ChoDefaultCommandLineArgAttribute, MemberInfo> > memberList = new List <Tuple <ChoDefaultCommandLineArgAttribute, MemberInfo> >(); foreach (MemberInfo memberInfo in memberInfos) { commandLineArgumentAttribute = null; defaultCommandLineArgAttribute = null; defaultCommandLineArgAttribute = commandLineArgumentAttribute = (ChoCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoCommandLineArgAttribute>(true); if (commandLineArgumentAttribute == null) { defaultCommandLineArgAttribute = posCommandLineArgAttribute = (ChoPositionalCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoPositionalCommandLineArgAttribute>(true); if (posCommandLineArgAttribute == null) { continue; } } memberList.Add(new Tuple <ChoDefaultCommandLineArgAttribute, MemberInfo>(defaultCommandLineArgAttribute, memberInfo)); } bool isEmptyShortName; memberList.Sort((x, y) => x.Item1.Order.CompareTo(y.Item1.Order)); MemberInfo memberInfo1 = null; foreach (Tuple <ChoDefaultCommandLineArgAttribute, MemberInfo> tuple in memberList) { memberInfo1 = tuple.Item2; commandLineArgumentAttribute = null; defaultCommandLineArgAttribute = null; defaultCommandLineArgAttribute = commandLineArgumentAttribute = (ChoCommandLineArgAttribute)memberInfo1.GetCustomAttribute <ChoCommandLineArgAttribute>(true); if (commandLineArgumentAttribute == null) { defaultCommandLineArgAttribute = posCommandLineArgAttribute = (ChoPositionalCommandLineArgAttribute)memberInfo1.GetCustomAttribute <ChoPositionalCommandLineArgAttribute>(true); if (posCommandLineArgAttribute == null) { continue; } } isEmptyShortName = !defaultCommandLineArgAttribute.ShortName.IsNull() && defaultCommandLineArgAttribute.ShortName.Length == 0; if (!defaultCommandLineArgAttribute.IsRequired) { builder.Append(" ["); } else { builder.Append(" "); } Type memberType = ChoType.GetMemberType(memberInfo1); if (memberType.IsNullableType()) { memberType = Nullable.GetUnderlyingType(memberType); } if (commandLineArgumentAttribute != null) { builder.Append("{0}{1}{2}".FormatString(cmdLineSwitchChar, commandLineArgumentAttribute.CommandLineSwitch, !isEmptyShortName ? cmdLineValueSeparator.ToString() : String.Empty)); } string description = null; if (commandLineArgumentAttribute != null) { description = commandLineArgumentAttribute.Description; } else if (posCommandLineArgAttribute != null) { description = posCommandLineArgAttribute.Description; } else { description = defaultCommandLineArgAttribute.Description; } if (_commandLineArgsObjectAttribute.GetDisplayDefaultValue()) { string defaultValue = null; defaultValue = GetDefaultValueText(memberInfo1); if (defaultValue != null && memberType != typeof(bool)) { description = "{0} [DEFAULT: {1}]".FormatString(description, defaultValue); } } if (commandLineArgumentAttribute != null) { whereBuilder.AppendFormat("{1}{3}{2}{0}", Environment.NewLine, GetCmdLineSwitches(cmdLineSwitchChar, commandLineArgumentAttribute), description.WrapLongLines(commandLineArgumentAttribute.DescriptionFormatLineSize, String.Empty, commandLineArgumentAttribute.DescriptionFormatLineBreakChar, commandLineArgumentAttribute.NoOfTabsSwitchDescFormatSeparator), "\t".Repeat(commandLineArgumentAttribute.NoOfTabsSwitchDescFormatSeparator)); } else if (posCommandLineArgAttribute != null) { whereBuilder.AppendFormat("{1}{3}{2}{0}", Environment.NewLine, defaultCommandLineArgAttribute.ShortName.IsNull() ? "Position{0}".FormatString(posCommandLineArgAttribute.Position) : defaultCommandLineArgAttribute.ShortName, description.WrapLongLines(posCommandLineArgAttribute.DescriptionFormatLineSize, String.Empty, posCommandLineArgAttribute.DescriptionFormatLineBreakChar, posCommandLineArgAttribute.NoOfTabsSwitchDescFormatSeparator), "\t".Repeat(posCommandLineArgAttribute.NoOfTabsSwitchDescFormatSeparator)); } else { whereBuilder.AppendFormat("{3}{2}{1}{0}", Environment.NewLine, description.WrapLongLines(defaultCommandLineArgAttribute.DescriptionFormatLineSize, String.Empty, defaultCommandLineArgAttribute.DescriptionFormatLineBreakChar, defaultCommandLineArgAttribute.NoOfTabsSwitchDescFormatSeparator), "\t".Repeat(defaultCommandLineArgAttribute.NoOfTabsSwitchDescFormatSeparator), DefaultCmdLineSwitch); } if (memberType == typeof(int)) { if (!defaultCommandLineArgAttribute.ShortName.IsNull()) { builder.Append(defaultCommandLineArgAttribute.ShortName); } else { builder.Append("<int>"); } } else if (memberType == typeof(uint)) { if (!defaultCommandLineArgAttribute.ShortName.IsNull()) { builder.Append(defaultCommandLineArgAttribute.ShortName); } else { builder.Append("<uint>"); } } else if (memberType == typeof(bool)) { builder.Remove(builder.Length - 1, 1); } else if (memberType == typeof(string)) { if (!defaultCommandLineArgAttribute.ShortName.IsNull()) { builder.Append(defaultCommandLineArgAttribute.ShortName); } else { builder.Append("<string>"); } } else if (memberType.IsEnum) { //builder.Append("{0}{1}{2}".FormatString(cmdLineSwitchChar, commandLineArgumentAttribute.CommandLineSwitch, !isEmptyShortName ? cmdLineValueSeparator.ToString() : String.Empty)); builder.Append("{"); bool first = true; foreach (FieldInfo field in memberType.GetFields()) { if (field.IsStatic) { if (first) { first = false; } else { builder.Append(" | "); } builder.Append(field.Name); } } builder.Append('}'); } else { if (!defaultCommandLineArgAttribute.ShortName.IsNull()) { builder.Append(defaultCommandLineArgAttribute.ShortName); } else { builder.Append("<Unknown>"); } } if (!defaultCommandLineArgAttribute.IsRequired) { builder.Append("]"); } } } if (!_commandLineArgsObjectAttribute.DoNotShowUsageDetail) { builder.Append(Environment.NewLine); builder.Append(Environment.NewLine); builder.Append(whereBuilder.ToString().Indent()); builder.Append(Environment.NewLine); foreach (ChoCommandLineArgAdditionalUsageAttribute commandLineArgAdditionalUsageAttribute in ChoType.GetAttributes <ChoCommandLineArgAdditionalUsageAttribute>(type)) { if (commandLineArgAdditionalUsageAttribute != null && !commandLineArgAdditionalUsageAttribute.AdditionalUsageText.IsNull()) { builder.Append(commandLineArgAdditionalUsageAttribute.AdditionalUsageText); builder.Append(Environment.NewLine); builder.Append(Environment.NewLine); } } } return(builder.ToString()); }
public static string GetUsage(object target) { ChoGuard.ArgumentNotNull(target, "Target"); StringBuilder builder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); ChoCommandLineParserSettings commandLineParserSettings = ChoCommandLineParserSettings.Me; char cmdLineValueSeperator = commandLineParserSettings.ValueSeperators != null && commandLineParserSettings.ValueSeperators.Length > 0 ? commandLineParserSettings.ValueSeperators[0] : ':'; char cmdLineSwitchChar = commandLineParserSettings.SwitchChars != null && commandLineParserSettings.SwitchChars.Length > 0 ? commandLineParserSettings.SwitchChars[0] : '-'; builder.Append(ChoApplication.EntryAssemblyFileName); MemberInfo[] memberInfos = ChoTypeMembersCache.GetAllMemberInfos(target.GetType()); if (memberInfos != null && memberInfos.Length > 0) { ChoCommandLineArgAttribute commandLineArgumentAttribute = null; ChoDefaultCommandLineArgAttribute defaultCommandLineArgAttribute = null; bool isEmptyShortName; foreach (MemberInfo memberInfo in memberInfos) { //if (ChoType.IsReadOnlyMember(memberInfo)) // continue; commandLineArgumentAttribute = null; defaultCommandLineArgAttribute = null; commandLineArgumentAttribute = (ChoCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoCommandLineArgAttribute>(true); if (commandLineArgumentAttribute == null) { defaultCommandLineArgAttribute = (ChoDefaultCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoDefaultCommandLineArgAttribute>(true); if (defaultCommandLineArgAttribute == null) { continue; } } else { defaultCommandLineArgAttribute = commandLineArgumentAttribute; } isEmptyShortName = !defaultCommandLineArgAttribute.ShortName.IsNull() && defaultCommandLineArgAttribute.ShortName.Length == 0; if (commandLineArgumentAttribute != null) { builder.Append(" {0}{1}{2}".FormatString(cmdLineSwitchChar, commandLineArgumentAttribute.CommandLineSwitch, !isEmptyShortName ? cmdLineValueSeperator.ToString() : String.Empty)); } else { builder.Append(" "); } Type memberType = ChoType.GetMemberType(memberInfo); if (defaultCommandLineArgAttribute.IsRequired) { if (!isEmptyShortName) { builder.Append("["); } } if (commandLineArgumentAttribute != null) { whereBuilder.AppendFormat("{1}{2}{4}{3}{0}", Environment.NewLine, cmdLineSwitchChar, commandLineArgumentAttribute.CommandLineSwitch, commandLineArgumentAttribute.Description.WrapLongLines(commandLineArgumentAttribute.DescriptionFormatLineSize, String.Empty, commandLineArgumentAttribute.DescriptionFormatLineBreakChar, commandLineArgumentAttribute.DescriptionFormatLineNoOfTabs), commandLineArgumentAttribute.SwitchValueSeperator); } else { whereBuilder.AppendFormat("{3}{2}{1}{0}", Environment.NewLine, defaultCommandLineArgAttribute.Description.WrapLongLines(defaultCommandLineArgAttribute.DescriptionFormatLineSize, String.Empty, defaultCommandLineArgAttribute.DescriptionFormatLineBreakChar, defaultCommandLineArgAttribute.DescriptionFormatLineNoOfTabs), defaultCommandLineArgAttribute.SwitchValueSeperator, DefaultCmdLineSwitch); } if (!defaultCommandLineArgAttribute.ShortName.IsNull()) { builder.Append(defaultCommandLineArgAttribute.ShortName); } else { if (memberType == typeof(int)) { builder.Append("<int>"); } else if (memberType == typeof(uint)) { builder.Append("<uint>"); } else if (memberType == typeof(bool)) { builder.Append("{True|False}"); } else if (memberType == typeof(string)) { builder.Append("<string>"); } else if (memberType.IsEnum) { builder.Append("{"); bool first = true; foreach (FieldInfo field in memberType.GetFields()) { if (field.IsStatic) { if (first) { first = false; } else { builder.Append('|'); } builder.Append(field.Name); } } builder.Append('}'); } else { builder.Append("<Unknown>"); } } if (defaultCommandLineArgAttribute.IsRequired) { if (!isEmptyShortName) { builder.Append("]"); } } } } builder.Append(Environment.NewLine); builder.Append(Environment.NewLine); builder.Append("Where"); builder.Append(Environment.NewLine); builder.Append(whereBuilder.ToString().Indent()); builder.Append(Environment.NewLine); if (target is ChoCommandLineArgObject) { string additionalUsageText = ((ChoCommandLineArgObject)target).AdditionalUsageText; if (!additionalUsageText.IsNullOrWhiteSpace()) { builder.Append(additionalUsageText); builder.Append(Environment.NewLine); builder.Append(Environment.NewLine); } } return(builder.ToString()); }
public static void Load(object target, string[] commandLineArgs) { ChoGuard.ArgumentNotNull(target, "Target"); ChoCommandLineArgObject commandLineArgObject = target as ChoCommandLineArgObject; Exception exception = null; if (commandLineArgObject != null) { commandLineArgObject.CommandLineArgs = commandLineArgs; } using (ChoCommandLineArgParser commandLineArgParser = new ChoCommandLineArgParser(commandLineArgs)) { commandLineArgParser.UnrecognizedCommandLineArgFound += ((sender, eventArgs) => { if (commandLineArgObject != null) { commandLineArgObject.OnUnrecognizedCommandLineArgFound(eventArgs); } }); commandLineArgParser.Parse(); if (commandLineArgParser.IsUsageArgSpecified) { throw new ChoCommandLineArgUsageException(GetUsage(target)); } string cmdLineSwitch = null; if (commandLineArgObject == null || !commandLineArgObject.OnBeforeCommandLineArgObjectLoaded(commandLineArgs)) { MemberInfo[] memberInfos = ChoType.GetMemberInfos(target.GetType(), typeof(ChoCommandLineArgAttribute)); MemberInfo[] memberInfos1 = ChoType.GetMemberInfos(target.GetType(), typeof(ChoDefaultCommandLineArgAttribute)); ChoCommandLineParserSettings commandLineParserSettings = ChoCommandLineParserSettings.Me; memberInfos = memberInfos.Concat(memberInfos1).ToArray(); if (memberInfos != null && memberInfos.Length > 0) { foreach (MemberInfo memberInfo in memberInfos) { ChoCommandLineArgAttribute commandLineArgumentAttribute = (ChoCommandLineArgAttribute)memberInfo.GetCustomAttribute <ChoCommandLineArgAttribute>(true); if (commandLineArgumentAttribute == null) { continue; } cmdLineSwitch = commandLineArgumentAttribute.CommandLineSwitch; bool isSwitchSpecified = IsSwitchSpecified(commandLineArgParser.Switches, cmdLineSwitch, commandLineParserSettings.IgnoreCase); exception = ExtractNPopulateValue(target, memberInfo, cmdLineSwitch, commandLineArgParser, commandLineArgObject, isSwitchSpecified); if (exception != null) { break; } } if (exception == null) { cmdLineSwitch = DefaultCmdLineSwitch; MemberInfo defaultMemberInfo = GetMemberForDefaultSwitch(memberInfos1); exception = ExtractNPopulateValue(target, defaultMemberInfo, DefaultCmdLineSwitch, commandLineArgParser, commandLineArgObject, false); } } } if (commandLineArgObject != null) { if (exception != null) { if (!commandLineArgObject.OnCommandLineArgObjectLoadError(commandLineArgs, exception)) { throw new ChoCommandLineArgException("Found exception while loading `{3}` command line argument. {0}{0}{2}{0}{0}{1}".FormatString( Environment.NewLine, GetUsage(target), exception.Message, cmdLineSwitch), exception); } } else { commandLineArgObject.OnAfterCommandLineArgObjectLoaded(commandLineArgs); } } } }
public static void Load(object target, string[] commandLineArgs) { ChoGuard.ArgumentNotNull(target, "Target"); ChoCommandLineArgObject commandLineArgObject = target as ChoCommandLineArgObject; if (commandLineArgObject == null) { throw new ChoApplicationException("Target is not ChoCommandLineArgObject."); } ChoCommandLineArgObjectAttribute commandLineArgumentsObjectAttribute = commandLineArgObject.GetType().GetCustomAttribute(typeof(ChoCommandLineArgObjectAttribute)) as ChoCommandLineArgObjectAttribute; if (commandLineArgumentsObjectAttribute == null) { throw new ChoApplicationException("Missing ChoCommandLineArgObjectAttribute. Must be specified."); } ChoCommandLineParserSettings commandLineParserSettings = ChoCommandLineParserSettings.Me; bool isUsageAvail = !commandLineArgumentsObjectAttribute.Silent; bool showUsageIfEmpty = commandLineArgumentsObjectAttribute.GetShowUsageIfEmpty(); if (commandLineArgs.IsNullOrEmpty() && showUsageIfEmpty) { if (isUsageAvail) { throw new ChoCommandLineArgUsageException(commandLineArgObject.GetUsage()); } } Exception exception = null; using (ChoCommandLineArgParser commandLineArgParser = new ChoCommandLineArgParser()) { commandLineArgParser.UnrecognizedCommandLineArgFound += ((sender, eventArgs) => { if (commandLineArgObject != null) { commandLineArgObject.RaiseUnrecognizedCommandLineArgFound(eventArgs); } }); commandLineArgParser.Parse(commandLineArgs); if (commandLineArgObject != null) { commandLineArgObject.CommandLineArgs = commandLineArgs; } if ((commandLineArgParser.IsUsageArgSpecified && commandLineArgumentsObjectAttribute.UsageSwitch.IsNullOrWhiteSpace()) || (!commandLineArgumentsObjectAttribute.UsageSwitch.IsNullOrWhiteSpace() && HasUsageSwitchSpecified(commandLineArgParser, commandLineArgumentsObjectAttribute)) ) { if (isUsageAvail) { throw new ChoCommandLineArgUsageException(commandLineArgObject.GetUsage()); } } string cmdLineSwitch = null; if (commandLineArgObject == null || !commandLineArgObject.RaiseBeforeCommandLineArgObjectLoaded(commandLineArgs)) { MemberInfo[] memberInfos = ChoType.GetMemberInfos(target.GetType(), typeof(ChoCommandLineArgAttribute)); MemberInfo[] memberInfos1 = ChoType.GetMemberInfos(target.GetType(), typeof(ChoPositionalCommandLineArgAttribute)); memberInfos = memberInfos1.Concat(memberInfos).ToArray(); if (memberInfos != null && memberInfos.Length > 0) { foreach (MemberInfo memberInfo in memberInfos) { cmdLineSwitch = GetCmdLineSwitch(memberInfo); exception = ExtractNPopulateValue(commandLineArgObject, memberInfo, commandLineArgParser); if (isUsageAvail) { if (exception != null) { break; } } } } } if (commandLineArgObject != null) { if (exception != null) { if (!commandLineArgObject.RaiseCommandLineArgObjectLoadError(commandLineArgs, exception)) { if (isUsageAvail) { if (exception is ChoCommandLineArgException) { throw exception; } else { throw new ChoCommandLineArgException("Found exception while loading `{2}` command line argument. {0}{0}{1}".FormatString( Environment.NewLine, exception.Message, cmdLineSwitch), commandLineArgObject.GetUsage(), exception); } } } } else { commandLineArgObject.RaiseAfterCommandLineArgObjectLoaded(commandLineArgs); } } } }