Skip to content

Commit 3f4b86d

Browse files
committed
added back the Setup<T>(string, string) method to ease the crossover - made it obsolete and thrown some tests to ensure it behaves the same
1 parent b015d3c commit 3f4b86d

File tree

5 files changed

+214
-121
lines changed

5 files changed

+214
-121
lines changed

FluentCommandLineParser.Tests/FluentCommandLineParserTests.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,72 @@ public void Ensure_Long_Options_Are_Case_Sensitive()
755755

756756
#endregion
757757

758+
#region Obsolete
759+
760+
[Test]
761+
public void Ensure_Obsolete_Setup_With_Only_Short_Option()
762+
{
763+
var parser = CreateFluentParser();
764+
parser.Setup<string>("s", null);
765+
var option = parser.Options.Single();
766+
Assert.IsNull(option.LongName);
767+
Assert.AreEqual("s", option.ShortName);
768+
}
769+
770+
[Test]
771+
public void Ensure_Obsolete_Setup_With_Only_Long_Option()
772+
{
773+
var parser = CreateFluentParser();
774+
parser.Setup<string>(null, "long");
775+
var option = parser.Options.Single();
776+
Assert.AreEqual("long", option.LongName);
777+
Assert.IsNull(option.ShortName);
778+
}
779+
780+
[Test]
781+
public void Ensure_Obsolete_Setup_With_Short_And_Long_Option()
782+
{
783+
var parser = CreateFluentParser();
784+
parser.Setup<string>("s", "long");
785+
var option = parser.Options.Single();
786+
Assert.AreEqual("long", option.LongName);
787+
Assert.AreEqual("s", option.ShortName);
788+
}
789+
790+
[Test]
791+
[ExpectedException(typeof(InvalidOptionNameException))]
792+
public void Ensure_Obsolete_Setup_Does_Not_Allow_Null_Short_And_Long_Options()
793+
{
794+
var parser = CreateFluentParser();
795+
parser.Setup<string>(null, null);
796+
}
797+
798+
[Test]
799+
[ExpectedException(typeof(InvalidOptionNameException))]
800+
public void Ensure_Obsolete_Setup_Does_Not_Allow_Empty_Short_And_Long_Options()
801+
{
802+
var parser = CreateFluentParser();
803+
parser.Setup<string>(string.Empty, string.Empty);
804+
}
805+
806+
[Test]
807+
[ExpectedException(typeof(InvalidOptionNameException))]
808+
public void Ensure_Obsolete_Setup_Does_Not_Allow_Short_Option_With_More_Than_One_Char()
809+
{
810+
var parser = CreateFluentParser();
811+
parser.Setup<string>("ab", null);
812+
}
813+
814+
[Test]
815+
[ExpectedException(typeof(InvalidOptionNameException))]
816+
public void Ensure_Obsolete_Setup_Does_Not_Allow_Long_Option_With_One_Char()
817+
{
818+
var parser = CreateFluentParser();
819+
parser.Setup<string>(null, "s");
820+
}
821+
822+
#endregion
823+
758824
#endregion Top Level Tests
759825

760826
#region Duplicate Options Tests

FluentCommandLineParser.Tests/Internals/CommandLineOptionTests.cs

Lines changed: 106 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void Ensure_Can_Be_Constructed_With_Null_ShortName_And_Valid_LongName()
9898

9999
var cmdOption = new CommandLineOption<object>(expectedShortName, expectedLongName, mockParser);
100100

101-
Assert.AreEqual(expectedShortName, cmdOption.ShortName, "Could not instantiate with null ShortName");
101+
Assert.AreEqual(expectedShortName, cmdOption.ShortName, "Could not instantiate with null ShortName");
102102
}
103103

104104
[Test]
@@ -110,7 +110,7 @@ public void Ensure_Can_Be_Constructed_With_Empty_ShortName_And_Valid_LongName()
110110

111111
var cmdOption = new CommandLineOption<object>(expectedShortName, expectedLongName, mockParser);
112112

113-
Assert.AreEqual(expectedShortName, cmdOption.ShortName, "Could not instantiate with empty ShortName");
113+
Assert.AreEqual(expectedShortName, cmdOption.ShortName, "Could not instantiate with empty ShortName");
114114
}
115115

116116
[Test]
@@ -122,117 +122,112 @@ public void Ensure_Can_Be_Constructed_With_Whitespace_Only_ShortName_And_Valid_L
122122

123123
var cmdOption = new CommandLineOption<object>(expectedShortName, expectedLongName, mockParser);
124124

125-
Assert.AreEqual(expectedShortName, cmdOption.ShortName, "Could not instantiate with whitespace only ShortName");
125+
Assert.AreEqual(expectedShortName, cmdOption.ShortName, "Could not instantiate with whitespace only ShortName");
126126
}
127127

128-
[Test]
129-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
130-
public void Ensure_Cannot_Be_Constructed_With_Null_ShortName_And_Null_LongName()
131-
{
132-
const string invalidShortName = null;
133-
const string invalidLongName = null;
134-
135-
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
136-
137-
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
138-
}
139-
140-
[Test]
141-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
142-
public void Ensure_Cannot_Be_Constructed_With_Empty_ShortName_And_Null_LongName()
143-
{
144-
const string invalidShortName = "";
145-
const string invalidLongName = null;
146-
147-
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
148-
149-
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
150-
}
151-
152-
[Test]
153-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
154-
public void Ensure_Cannot_Be_Constructed_With_WhiteSpaceOnly_ShortName_And_Null_LongName()
155-
{
156-
const string invalidShortName = " ";
157-
const string invalidLongName = null;
158-
159-
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
160-
161-
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
162-
}
163-
164-
[Test]
165-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
166-
public void Ensure_Cannot_Be_Constructed_With_Null_ShortName_And_Empty_LongName()
167-
{
168-
const string invalidShortName = null;
169-
const string invalidLongName = "";
170-
171-
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
172-
173-
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
174-
}
175-
176-
[Test]
177-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
178-
public void Ensure_Cannot_Be_Constructed_With_Empty_ShortName_And_Empty_LongName()
179-
{
180-
const string invalidShortName = "";
181-
const string invalidLongName = "";
182-
183-
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
184-
185-
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
186-
}
187-
188-
[Test]
189-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
190-
public void Ensure_Cannot_Be_Constructed_With_WhiteSpaceOnly_ShortName_And_Empty_LongName()
191-
{
192-
const string invalidShortName = " ";
193-
const string invalidLongName = "";
194-
195-
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
196-
197-
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
198-
}
199-
200-
[Test]
201-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
202-
public void Ensure_Cannot_Be_Constructed_With_Null_ShortName_And_WhiteSpaceOnly_LongName()
203-
{
204-
const string invalidShortName = null;
205-
const string invalidLongName = " ";
206-
207-
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
208-
209-
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
210-
}
211-
212-
[Test]
213-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
214-
public void Ensure_Cannot_Be_Constructed_With_Empty_ShortName_And_WhiteSpaceOnly_LongName()
215-
{
216-
const string invalidShortName = "";
217-
const string invalidLongName = " ";
218-
219-
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
220-
221-
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
222-
}
223-
224-
[Test]
225-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
226-
public void Ensure_Cannot_Be_Constructed_With_WhiteSpaceOnly_ShortName_And_WhiteSpaceOnly_LongName()
227-
{
228-
const string invalidShortName = " ";
229-
const string invalidLongName = " ";
230-
231-
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
232-
233-
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
234-
}
235-
128+
[Test]
129+
[ExpectedException(typeof(ArgumentOutOfRangeException))]
130+
public void Ensure_Cannot_Be_Constructed_With_Null_ShortName_And_Null_LongName()
131+
{
132+
const string invalidShortName = null;
133+
const string invalidLongName = null;
134+
135+
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
136+
137+
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
138+
}
139+
140+
[Test]
141+
[ExpectedException(typeof(ArgumentOutOfRangeException))]
142+
public void Ensure_Cannot_Be_Constructed_With_Empty_ShortName_And_Null_LongName()
143+
{
144+
const string invalidShortName = "";
145+
const string invalidLongName = null;
146+
147+
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
148+
149+
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
150+
}
151+
152+
[Test]
153+
[ExpectedException(typeof(ArgumentOutOfRangeException))]
154+
public void Ensure_Cannot_Be_Constructed_With_WhiteSpaceOnly_ShortName_And_Null_LongName()
155+
{
156+
const string invalidShortName = " ";
157+
const string invalidLongName = null;
158+
159+
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
160+
161+
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
162+
}
163+
164+
[Test]
165+
public void Ensure_Can_Be_Constructed_With_Null_ShortName_And_Empty_LongName()
166+
{
167+
const string invalidShortName = null;
168+
const string invalidLongName = "";
169+
170+
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
171+
172+
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
173+
}
174+
175+
[Test]
176+
public void Ensure_Can_Be_Constructed_With_Empty_ShortName_And_Empty_LongName()
177+
{
178+
const string invalidShortName = "";
179+
const string invalidLongName = "";
180+
181+
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
182+
183+
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
184+
}
185+
186+
[Test]
187+
public void Ensure_Can_Be_Constructed_With_WhiteSpaceOnly_ShortName_And_Empty_LongName()
188+
{
189+
const string invalidShortName = " ";
190+
const string invalidLongName = "";
191+
192+
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
193+
194+
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
195+
}
196+
197+
[Test]
198+
public void Ensure_Can_Be_Constructed_With_Null_ShortName_And_WhiteSpaceOnly_LongName()
199+
{
200+
const string invalidShortName = null;
201+
const string invalidLongName = " ";
202+
203+
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
204+
205+
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
206+
}
207+
208+
[Test]
209+
public void Ensure_Can_Be_Constructed_With_Empty_ShortName_And_WhiteSpaceOnly_LongName()
210+
{
211+
const string invalidShortName = "";
212+
const string invalidLongName = " ";
213+
214+
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
215+
216+
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
217+
}
218+
219+
[Test]
220+
[ExpectedException(typeof(ArgumentOutOfRangeException))]
221+
public void Ensure_Cannot_Be_Constructed_With_WhiteSpaceOnly_ShortName_And_WhiteSpaceOnly_LongName()
222+
{
223+
const string invalidShortName = " ";
224+
const string invalidLongName = " ";
225+
226+
var mockParser = Mock.Of<ICommandLineOptionParser<object>>();
227+
228+
new CommandLineOption<object>(invalidShortName, invalidLongName, mockParser);
229+
}
230+
236231
[Test]
237232
[ExpectedException(typeof(ArgumentNullException))]
238233
public void Ensure_Cannot_Be_Constructed_With_Null_Parser()

FluentCommandLineParser/FluentCommandLineParser.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,25 @@ public ICommandLineOptionFluent<T> Setup<T>(char shortOption, string longOption)
130130
return SetupInternal<T>(shortOption.ToString(CultureInfo.InvariantCulture), longOption);
131131
}
132132

133+
/// <summary>
134+
/// Setup a new <see cref="ICommandLineOptionFluent{T}"/> using the specified short and long Option name.
135+
/// </summary>
136+
/// <param name="shortOption">The short name for the Option. This must not be <c>whitespace</c> or a control character.</param>
137+
/// <param name="longOption">The long name for the Option. This must not be <c>null</c>, <c>empty</c> or only <c>whitespace</c>.</param>
138+
/// <returns></returns>
139+
/// <exception cref="OptionAlreadyExistsException">
140+
/// A Option with the same <paramref name="shortOption"/> name or <paramref name="longOption"/> name already exists in the <see cref="IFluentCommandLineParser"/>.
141+
/// </exception>
142+
/// <exception cref="InvalidOptionNameException">
143+
/// Either <paramref name="shortOption"/> or <paramref name="longOption"/> are not valid. <paramref name="shortOption"/> must not be <c>whitespace</c>
144+
/// or a control character. <paramref name="longOption"/> must not be <c>null</c>, <c>empty</c> or only <c>whitespace</c>.
145+
/// </exception>
146+
[Obsolete("Use new overload Setup<T>(char, string) to specify both a short and long option name instead.")]
147+
public ICommandLineOptionFluent<T> Setup<T>(string shortOption, string longOption)
148+
{
149+
return SetupInternal<T>(shortOption, longOption);
150+
}
151+
133152
private ICommandLineOptionFluent<T> SetupInternal<T>(string shortOption, string longOption)
134153
{
135154
var argOption = this.OptionFactory.CreateOption<T>(shortOption, longOption);

FluentCommandLineParser/IFluentCommandLineParser.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ public interface IFluentCommandLineParser
4949
/// </exception>
5050
ICommandLineOptionFluent<T> Setup<T>(char shortOption, string longOption);
5151

52+
/// <summary>
53+
/// Setup a new <see cref="ICommandLineOptionFluent{T}"/> using the specified short and long Option name.
54+
/// </summary>
55+
/// <param name="shortOption">The short name for the Option. This must not be <c>whitespace</c> or a control character.</param>
56+
/// <param name="longOption">The long name for the Option. This must not be <c>null</c>, <c>empty</c> or only <c>whitespace</c>.</param>
57+
/// <returns></returns>
58+
/// <exception cref="OptionAlreadyExistsException">
59+
/// A Option with the same <paramref name="shortOption"/> name or <paramref name="longOption"/> name already exists in the <see cref="IFluentCommandLineParser"/>.
60+
/// </exception>
61+
/// <exception cref="InvalidOptionNameException">
62+
/// Either <paramref name="shortOption"/> or <paramref name="longOption"/> are not valid. <paramref name="shortOption"/> must not be <c>whitespace</c>
63+
/// or a control character. <paramref name="longOption"/> must not be <c>null</c>, <c>empty</c> or only <c>whitespace</c>.
64+
/// </exception>
65+
[Obsolete("Use new overload Setup<T>(char, string) to specify both a short and long option name instead.")]
66+
ICommandLineOptionFluent<T> Setup<T>(string shortOption, string longOption);
67+
5268
/// <summary>
5369
/// Setup a new <see cref="ICommandLineOptionFluent{T}"/> using the specified short Option name.
5470
/// </summary>

0 commit comments

Comments
 (0)