/// <summary> /// Prints option aliases and its description /// </summary> /// <param name="option">Option to print the help text for</param> private void PrintOptionDefinition( Option option ) { var signature = GenerateOptionSignature( option ); Builder.Append( signature ); PrintOptionDescription( option, signature.Length ); }
/// <summary> /// Prints option description to a justified block of text /// </summary> /// <param name="option">Option to print a description for</param> /// <param name="offset">Length of the option signature</param> private void PrintOptionDescription( Option option, int offset ) { if ( offset > DescriptionOffset ) { // Option signature is longer than desired description offset Builder.AppendLine(); offset = 0; } // Split description into paragraphs and print them independently option.Description.Split( NEWLINE ) .ToList() .ForEach( description => { PrintDescriptionParagraph( description, offset ); offset = 0; // reset offset for additional paragraphs } ); }
/// <summary> /// Generates a signature for an option, i.e. list of option aliases /// with argument indications included /// </summary> /// <param name="option">Option to create a signature for</param> /// <returns>Signature of the option</returns> private string GenerateOptionSignature( Option option ) { var partialSignatures = option.Aliases.Select( alias => GeneratePartialOptionSignature( option, alias ) ); return String.Join( OPTION_ALIASES_SEPARATOR, partialSignatures ); }
/// <summary> /// Generates a signature for a single option alias /// </summary> /// <param name="option"> /// Option to create a partial signature for /// </param> /// <param name="alias">Particular option alias</param> /// <returns>Signature for a single option alias</returns> private string GeneratePartialOptionSignature( Option option, OptionAlias alias) { return alias + GenerateOptionArgument( option, alias.Type ); }
/// <summary> /// Generates an argument part of a partial option signature /// </summary> /// <param name="option">Parent option</param> /// <param name="type">Short or Long option type</param> /// <returns></returns> private string GenerateOptionArgument( Option option, OptionType type ) { if ( option.Argument == null ) { return ""; } // Combine a prefix with the Argument name var prefix = type == OptionType.Short ? SHORT_ARGUMENT_SEPARATOR : LONG_ARGUMENT_SEPARATOR; var signature = prefix + String.Format( ARGUMENT_NAME_FORMAT, option.Argument.Name ); // Apply special formatting if the Argument is optional if ( option.Argument.Optional ) { signature = String.Format( OPTIONAL_ARGUMENT_FORMAT, signature ); } return signature; }
/// <summary> /// Inserts a value for specified Option to the CommandLine /// </summary> /// <param name="option">Detected Option</param> /// <param name="value">Option value</param> private void SetCommandLineOptionValue( Option option, object value ) { option.Aliases.ForEach( alias => { CommandLine.Options[ alias ] = value; } ); }
/// <summary> /// Inserts a "is set" marker to the CommandLine for input Option /// </summary> /// <param name="option">Option that is detected in arguments</param> private void SetCommandLineOptionIsSet( Option option ) { option.Aliases.ForEach( alias => { if ( !CommandLine.Options.ContainsKey( alias ) ) { CommandLine.Options[ alias ] = null; // is set indicator } } ); }
/// <summary> /// Parse and process short option argument /// </summary> /// <param name="optionAlias">Option alias.</param> /// <param name="option">Option.</param> private void ProcessShortOptionArgument( OptionAlias optionAlias, Option option) { // Remove processed option from arguments list Args.Dequeue(); if ( option.Argument == null ) { // No argument specified for the Option return; } var stringValue = Args.Any() ? Args.First() : ""; // An argument is expected and its value is set dynamic value = null; string exceptionMessage = null; try { if ( stringValue.Length == 0 ) { throw new ArgumentNullException(); } value = option.Argument .Parse( stringValue, Converter ); option.Argument.AssertConditions( value ); } catch ( ArgumentNullException ) { // Argument is mandatory and no value is provided exceptionMessage = "No option value specified for option {0}"; } catch ( ArgumentOutOfRangeException ) { // Value does not satisfy Argument Conditions exceptionMessage = "Value for option {0} does not meet conditions"; } catch ( Exception ) { // Value cannot be converted to specified format exceptionMessage = "Value for option {0} is not of required type"; } if ( exceptionMessage != null ) { if ( option.Argument.DefaultValueIsSet ) { // Provided value is not valid and a default value exists value = option.Argument.DefaultValue; } else if ( !option.Argument.Optional ) { // Provided value is not valid and it is mandatory throw new ParseException( exceptionMessage, optionAlias.ToString() ); } } else { // Remove correct Option argument from the argument list Args.Dequeue(); } option.Argument.InvokeActions( value ); SetCommandLineOptionValue( option, value ); }
/// <summary> /// Parse and process long option argument. /// </summary> /// <param name="optionAlias">Option alias.</param> /// <param name="option">Option.</param> private void ProcessLongOptionArgument( OptionAlias optionAlias, Option option) { var stringValue = ExtractLongOptionValue( Args.First() ); if ( stringValue.Length > 0 && option.Argument == null ) { // There is an Option value present, but it should not be there throw new ParseException( "Unexpected long option value: {0}", Args.First() ); } if ( stringValue.Length == 0 && option.Argument != null ) { // An argument or default value is expected if ( !option.Argument.Optional ) { // Argument is mandatory and no value is provided throw new ParseException( "No option value specified for option {0}", Args.First() ); } option.Argument.InvokeActions( option.Argument.DefaultValue ); SetCommandLineOptionValue( option, option.Argument.DefaultValue ); } else if ( stringValue.Length > 0 && option.Argument != null ) { // An argument is expected and its value is set dynamic value; try { value = option.Argument .Parse( stringValue, Converter ); option.Argument.AssertConditions( value ); } catch ( ArgumentOutOfRangeException ) { // Value does not satisfy Argument Conditions throw new ParseException( "Value for option {0} does not meet conditions", optionAlias.ToString() ); } catch ( Exception ) { // Value cannot be converted to specified format throw new ParseException( "Value for option {0} is not of required type", optionAlias.ToString() ); } option.Argument.InvokeActions( value ); SetCommandLineOptionValue( option, value ); } // Remove processed option from arguments list Args.Dequeue(); }