Skip to content

Commit a62cdc5

Browse files
committed
Config rework (part 3)
1 parent 56a81cd commit a62cdc5

File tree

4 files changed

+50
-60
lines changed

4 files changed

+50
-60
lines changed

SmartImage/Core/ConfigComponents.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ internal static IDictionary<string, string> ConvertComponentsToMap(object obj)
9898
}
9999

100100

101-
internal static void WriteComponentsToFile(object obj, string dest) => Collections.WriteDictionary(ConvertComponentsToMap(obj), dest);
101+
internal static void WriteComponentsToFile(object obj, string dest) =>
102+
Collections.WriteDictionary(ConvertComponentsToMap(obj), dest);
102103

103104

104105
internal static T ReadComponentMapValue<T>(object obj, IDictionary<string, string> cfg, string mname)
@@ -194,28 +195,30 @@ internal static T ReadComponentMapValue<T>(IDictionary<string, string> cfg, stri
194195
var parse = ParseComponentValue<T>(rawValue);
195196
return parse;
196197
}
197-
198-
199-
internal static void ReadComponentFromArgument(object obj,IEnumerator<string> argEnumerator)
198+
199+
200+
internal static void ReadComponentFromArgument(object obj, IEnumerator<string> argEnumerator)
200201
{
201-
var argName = argEnumerator.Current;
202-
203-
var x = GetMembers(obj);
204-
205-
var correspondingComponent = x.FirstOrDefault(y => y.Attribute.ArgumentName == argName);
202+
var parameterName = argEnumerator.Current;
203+
204+
var members = GetMembers(obj);
206205

207-
if (correspondingComponent == default) {
206+
// Corresponding component
207+
var component = members.FirstOrDefault(y => y.Attribute.ParameterName == parameterName);
208+
209+
if (component == default) {
208210
return;
209211
}
210-
212+
211213
argEnumerator.MoveNext();
212-
213-
string argValue2 = argEnumerator.Current;
214-
var bf = correspondingComponent.Member.GetBackingField();
215-
216-
var value = ConfigComponents.ParseComponentValue(argValue2, bf.FieldType);
217-
218-
bf.SetValue(obj, value);
214+
215+
string argValueRaw = argEnumerator.Current;
216+
217+
var field = component.Member.GetBackingField();
218+
219+
var value = ParseComponentValue(argValueRaw, field.FieldType);
220+
221+
field.SetValue(obj, value);
219222
}
220223
}
221224
}

SmartImage/Core/SearchConfig.cs

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,28 @@ private SearchConfig()
6060
/// <summary>
6161
/// Engines to use for searching
6262
/// </summary>
63-
[field: ConfigComponent("search_engines", SearchEngineOptions.All, "--search-engines")]
63+
[field: ConfigComponent("search_engines", "--search-engines", SearchEngineOptions.All)]
6464
public SearchEngineOptions SearchEngines { get; set; }
6565

6666
/// <summary>
6767
/// Engines whose results should be opened in the browser
6868
/// </summary>
69-
[field: ConfigComponent("priority_engines", SearchEngineOptions.Auto)]
69+
[field: ConfigComponent("priority_engines", "--priority-engines", SearchEngineOptions.Auto)]
7070
public SearchEngineOptions PriorityEngines { get; set; }
7171

7272
/// <summary>
7373
/// <see cref="ImgurClient" /> API key
7474
/// </summary>
75-
[field: ConfigComponent("imgur_client_id", Strings.Empty)]
75+
[field: ConfigComponent("imgur_client_id", "--saucenao-auth", Strings.Empty)]
7676
public string ImgurAuth { get; set; }
7777

7878
/// <summary>
7979
/// <see cref="SauceNaoEngine" /> API key
8080
/// </summary>
81-
[field: ConfigComponent("saucenao_key", Strings.Empty)]
81+
[field: ConfigComponent("saucenao_key", "--imgur-auth", Strings.Empty)]
8282
public string SauceNaoAuth { get; set; }
8383

84+
8485
/// <summary>
8586
/// Whether to save passed in arguments (via CLI) to the config file upon exit
8687
/// </summary>
@@ -123,10 +124,7 @@ private void ReadFromFile()
123124
}
124125

125126

126-
public void Reset()
127-
{
128-
ConfigComponents.ResetComponents(this);
129-
}
127+
public void Reset() => ConfigComponents.ResetComponents(this);
130128

131129

132130
public void SaveFile()
@@ -225,44 +223,24 @@ private void ReadFromArguments()
225223
return;
226224
}
227225

228-
var argQueue = new Queue<string>(args);
226+
var argQueue = new Queue<string>(args);
227+
229228
using var argEnumerator = argQueue.GetEnumerator();
230229

231230
while (argEnumerator.MoveNext()) {
232-
string argValue = argEnumerator.Current;
231+
string parameterName = argEnumerator.Current;
233232

234-
// todo: add to ConfigComponentAttribute
235233
ConfigComponents.ReadComponentFromArgument(this, argEnumerator);
236-
237-
238-
switch (argValue) {
239-
/*case "--search-engines":
240-
argEnumerator.MoveNext();
241-
string sestr = argEnumerator.Current;
242-
SearchEngines = ConfigComponents.ParseComponentValue<SearchEngineOptions>(sestr);
243-
break;
244-
case "--priority-engines":
245-
argEnumerator.MoveNext();
246-
string pestr = argEnumerator.Current;
247-
PriorityEngines = ConfigComponents.ParseComponentValue<SearchEngineOptions>(pestr);
248-
break;
249-
case "--saucenao-auth":
250-
argEnumerator.MoveNext();
251-
string snastr = argEnumerator.Current;
252-
SauceNaoAuth = snastr;
253-
break;
254-
case "--imgur-auth":
255-
argEnumerator.MoveNext();
256-
string imastr = argEnumerator.Current;
257-
ImgurAuth = imastr;
258-
break;*/
234+
235+
// Special cases
236+
switch (parameterName) {
237+
259238
case "--update-cfg":
260239
UpdateConfig = true;
261240
break;
262241

263-
264242
default:
265-
Image = argValue;
243+
Image = parameterName;
266244
break;
267245
}
268246
}

SmartImage/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static class Program
3535

3636
/*
3737
* --search-engines Iqdb,SauceNao --priority-engines Auto --update-cfg C:\Users\Deci\Desktop\fucking_epic.jpg
38+
* --search-engines All --priority-engines Auto C:\Users\Deci\Desktop\fucking_epic.jpg
3839
*/
3940

4041
private static void Main(string[] args)

SmartImage/Utilities/ConfigComponentAttribute.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,34 @@ internal sealed class ConfigComponentAttribute : Attribute
1717
{
1818
internal object DefaultValue { get; set; }
1919

20+
/// <summary>
21+
/// Component name
22+
/// </summary>
2023
internal string Id { get; set; }
2124

25+
2226
internal bool SetDefaultIfNull { get; set; }
2327

28+
29+
/// <summary>
30+
/// Parameter name
31+
/// </summary>
2432
[CanBeNull]
25-
internal string ArgumentName { get; set; }
33+
internal string ParameterName { get; set; }
2634

2735

28-
internal ConfigComponentAttribute(string id, object defaultValue, bool setDefaultIfNull, string argumentName)
36+
internal ConfigComponentAttribute(string id, string parameterName, object defaultValue, bool setDefaultIfNull)
2937
{
3038
Id = id;
3139
DefaultValue = defaultValue;
3240
SetDefaultIfNull = setDefaultIfNull;
33-
ArgumentName = argumentName;
41+
ParameterName = parameterName;
3442
}
3543

36-
internal ConfigComponentAttribute(string id, object defaultValue, [CanBeNull] string argumentName) : this(id,
37-
defaultValue, false, argumentName) { }
44+
internal ConfigComponentAttribute(string id, [CanBeNull] string parameterName, object defaultValue) : this(id,
45+
parameterName, defaultValue, false) { }
3846

3947

40-
internal ConfigComponentAttribute(string id, object defaultValue) : this(id, defaultValue, null) { }
48+
internal ConfigComponentAttribute(string id, object defaultValue) : this(id, null, defaultValue) { }
4149
}
4250
}

0 commit comments

Comments
 (0)