diff --git a/src/CommandLine/BaseAttribute.cs b/src/CommandLine/BaseAttribute.cs
index be0a3826..83379bb2 100644
--- a/src/CommandLine/BaseAttribute.cs
+++ b/src/CommandLine/BaseAttribute.cs
@@ -12,6 +12,7 @@ public abstract class BaseAttribute : Attribute
private int min;
private int max;
private object @default;
+ private string env;
private Infrastructure.LocalizableAttributeProperty helpText;
private string metaValue;
private Type resourceType;
@@ -87,6 +88,18 @@ public object Default
}
}
+ ///
+ /// Gets or sets mapped property env value.
+ ///
+ public string Env
+ {
+ get { return env; }
+ set
+ {
+ env = value;
+ }
+ }
+
///
/// Gets or sets a short description of this command line option. Usually a sentence summary.
///
diff --git a/src/CommandLine/Core/InstanceBuilder.cs b/src/CommandLine/Core/InstanceBuilder.cs
index dce377f1..438ad611 100644
--- a/src/CommandLine/Core/InstanceBuilder.cs
+++ b/src/CommandLine/Core/InstanceBuilder.cs
@@ -88,7 +88,7 @@ public static ParserResult Build(
T instance;
if(typeInfo.IsMutable() == true)
{
- instance = BuildMutable(factory, specPropsWithValue, setPropertyErrors);
+ instance = BuildMutable(factory, specPropsWithValue, setPropertyErrors, ignoreValueCase, parsingCulture);
}
else
{
@@ -126,7 +126,12 @@ public static ParserResult Build(
return result;
}
- private static T BuildMutable(Maybe> factory, IEnumerable specPropsWithValue, List setPropertyErrors )
+ private static T BuildMutable(
+ Maybe> factory,
+ IEnumerable specPropsWithValue,
+ List setPropertyErrors,
+ bool ignoreValueCase,
+ CultureInfo parsingCulture)
{
var mutable = factory.MapValueOrDefault(f => f(), () => Activator.CreateInstance());
@@ -141,7 +146,27 @@ private static T BuildMutable(Maybe> factory, IEnumerable sp.Value.IsNothing() && sp.Specification.DefaultValue.IsJust(),
+ sp => sp.Value.IsNothing() && sp.Specification.Env.Map(Environment.GetEnvironmentVariable).Map(n => !(n is null)).GetValueOrDefault(false),
+ sp => sp.Specification.Env
+ .Map(Environment.GetEnvironmentVariable)
+ .Bind(v =>
+ {
+ var isSequence = sp.Specification.TargetType == TargetType.Sequence;
+ if (isSequence)
+ {
+ throw new Exception($"Sequences not supported for options with \"Env\" as is the case with {sp.Property.Name}");
+ }
+ return TypeConverter
+ .ChangeType(new string[] { v }, sp.Specification.ConversionType, true, parsingCulture, ignoreValueCase);
+ })
+ .FromJustOrFail()
+ )
+ );
+
+ setPropertyErrors.AddRange(
+ mutable.SetProperties(
+ specPropsWithValue,
+ sp => sp.Value.IsNothing() && sp.Specification.DefaultValue.IsJust() && sp.Specification.Env.Map(Environment.GetEnvironmentVariable).Map(n => n is null).GetValueOrDefault(true),
sp => sp.Specification.DefaultValue.FromJustOrFail()
)
);
diff --git a/src/CommandLine/Core/OptionSpecification.cs b/src/CommandLine/Core/OptionSpecification.cs
index 77e7977f..3bfd4700 100644
--- a/src/CommandLine/Core/OptionSpecification.cs
+++ b/src/CommandLine/Core/OptionSpecification.cs
@@ -16,10 +16,10 @@ sealed class OptionSpecification : Specification
private readonly string group;
public OptionSpecification(string shortName, string longName, bool required, string setName, Maybe min, Maybe max,
- char separator, Maybe