@@ -290,6 +290,64 @@ protected virtual void Dispose(bool disposing)
290290 configHandle . SafeDispose ( ) ;
291291 }
292292
293+ /// <summary>
294+ /// Get a configuration value for the given key parts.
295+ /// <para>
296+ /// For example in order to get the value for this in a .git\config file:
297+ ///
298+ /// <code>
299+ /// [core]
300+ /// bare = true
301+ /// </code>
302+ ///
303+ /// You would call:
304+ ///
305+ /// <code>
306+ /// bool isBare = repo.Config.Get<bool>(new []{ "core", "bare" }).Value;
307+ /// </code>
308+ /// </para>
309+ /// </summary>
310+ /// <typeparam name="T">The configuration value type</typeparam>
311+ /// <param name="keyParts">The key parts</param>
312+ /// <returns>The <see cref="ConfigurationEntry{T}"/>, or null if not set</returns>
313+ public virtual ConfigurationEntry < T > Get < T > ( string [ ] keyParts )
314+ {
315+ Ensure . ArgumentNotNull ( keyParts , "keyParts" ) ;
316+
317+ return Get < T > ( string . Join ( "." , keyParts ) ) ;
318+ }
319+
320+ /// <summary>
321+ /// Get a configuration value for the given key parts.
322+ /// <para>
323+ /// For example in order to get the value for this in a .git\config file:
324+ ///
325+ /// <code>
326+ /// [difftool "kdiff3"]
327+ /// path = c:/Program Files/KDiff3/kdiff3.exe
328+ /// </code>
329+ ///
330+ /// You would call:
331+ ///
332+ /// <code>
333+ /// string where = repo.Config.Get<string>("difftool", "kdiff3", "path").Value;
334+ /// </code>
335+ /// </para>
336+ /// </summary>
337+ /// <typeparam name="T">The configuration value type</typeparam>
338+ /// <param name="firstKeyPart">The first key part</param>
339+ /// <param name="secondKeyPart">The second key part</param>
340+ /// <param name="thirdKeyPart">The third key part</param>
341+ /// <returns>The <see cref="ConfigurationEntry{T}"/>, or null if not set</returns>
342+ public virtual ConfigurationEntry < T > Get < T > ( string firstKeyPart , string secondKeyPart , string thirdKeyPart )
343+ {
344+ Ensure . ArgumentNotNullOrEmptyString ( firstKeyPart , "firstKeyPart" ) ;
345+ Ensure . ArgumentNotNullOrEmptyString ( secondKeyPart , "secondKeyPart" ) ;
346+ Ensure . ArgumentNotNullOrEmptyString ( thirdKeyPart , "thirdKeyPart" ) ;
347+
348+ return Get < T > ( new [ ] { firstKeyPart , secondKeyPart , thirdKeyPart } ) ;
349+ }
350+
293351 /// <summary>
294352 /// Get a configuration value for a key. Keys are in the form 'section.name'.
295353 /// <para>
@@ -366,6 +424,181 @@ public virtual ConfigurationEntry<T> Get<T>(string key, ConfigurationLevel level
366424 }
367425 }
368426
427+ /// <summary>
428+ /// Get a configuration value for the given key.
429+ /// </summary>
430+ /// <typeparam name="T">The configuration value type.</typeparam>
431+ /// <param name="key">The key</param>
432+ /// <returns>The configuration value, or the default value for the selected <see typeparamref="T"/>if not found</returns>
433+ public virtual T GetValueOrDefault < T > ( string key )
434+ {
435+ return ValueOrDefault ( Get < T > ( key ) , default ( T ) ) ;
436+ }
437+
438+ /// <summary>
439+ /// Get a configuration value for the given key,
440+ /// or <paramref name="defaultValue" /> if the key is not set.
441+ /// </summary>
442+ /// <typeparam name="T">The configuration value type.</typeparam>
443+ /// <param name="key">The key</param>
444+ /// <param name="defaultValue">The default value if the key is not set.</param>
445+ /// <returns>The configuration value, or the default value</returns>
446+ public virtual T GetValueOrDefault < T > ( string key , T defaultValue )
447+ {
448+ return ValueOrDefault ( Get < T > ( key ) , defaultValue ) ;
449+ }
450+
451+ /// <summary>
452+ /// Get a configuration value for the given key
453+ /// </summary>
454+ /// <typeparam name="T">The configuration value type.</typeparam>
455+ /// <param name="key">The key.</param>
456+ /// <param name="level">The configuration file into which the key should be searched for.</param>
457+ /// <returns>The configuration value, or the default value for <see typeparamref="T"/> if not found</returns>
458+ public virtual T GetValueOrDefault < T > ( string key , ConfigurationLevel level )
459+ {
460+ return ValueOrDefault ( Get < T > ( key , level ) , default ( T ) ) ;
461+ }
462+
463+ /// <summary>
464+ /// Get a configuration value for the given key,
465+ /// or <paramref name="defaultValue" /> if the key is not set.
466+ /// </summary>
467+ /// <typeparam name="T">The configuration value type.</typeparam>
468+ /// <param name="key">The key.</param>
469+ /// <param name="level">The configuration file into which the key should be searched for.</param>
470+ /// <param name="defaultValue">The selector used to generate a default value if the key is not set.</param>
471+ /// <returns>The configuration value, or the default value.</returns>
472+ public virtual T GetValueOrDefault < T > ( string key , ConfigurationLevel level , T defaultValue )
473+ {
474+ return ValueOrDefault ( Get < T > ( key , level ) , defaultValue ) ;
475+ }
476+
477+ /// <summary>
478+ /// Get a configuration value for the given key parts
479+ /// </summary>
480+ /// <typeparam name="T">The configuration value type.</typeparam>
481+ /// <param name="keyParts">The key parts.</param>
482+ /// <returns>The configuration value, or the default value for<see typeparamref="T"/> if not found</returns>
483+ public virtual T GetValueOrDefault < T > ( string [ ] keyParts )
484+ {
485+ return ValueOrDefault ( Get < T > ( keyParts ) , default ( T ) ) ;
486+ }
487+
488+ /// <summary>
489+ /// Get a configuration value for the given key parts,
490+ /// or <paramref name="defaultValue" /> if the key is not set.
491+ /// </summary>
492+ /// <typeparam name="T">The configuration value type.</typeparam>
493+ /// <param name="keyParts">The key parts.</param>
494+ /// <param name="defaultValue">The default value if the key is not set.</param>
495+ /// <returns>The configuration value, or the default value.</returns>
496+ public virtual T GetValueOrDefault < T > ( string [ ] keyParts , T defaultValue )
497+ {
498+ return ValueOrDefault ( Get < T > ( keyParts ) , defaultValue ) ;
499+ }
500+
501+ /// <summary>
502+ /// Get a configuration value for the given key parts.
503+ /// </summary>
504+ /// <typeparam name="T">The configuration value type.</typeparam>
505+ /// <param name="firstKeyPart">The first key part.</param>
506+ /// <param name="secondKeyPart">The second key part.</param>
507+ /// <param name="thirdKeyPart">The third key part.</param>
508+ /// <returns>The configuration value, or the default value for the selected <see typeparamref="T"/> if not found</returns>
509+ public virtual T GetValueOrDefault < T > ( string firstKeyPart , string secondKeyPart , string thirdKeyPart )
510+ {
511+ return ValueOrDefault ( Get < T > ( firstKeyPart , secondKeyPart , thirdKeyPart ) , default ( T ) ) ;
512+ }
513+
514+ /// <summary>
515+ /// Get a configuration value for the given key parts,
516+ /// or <paramref name="defaultValue" /> if the key is not set.
517+ /// </summary>
518+ /// <typeparam name="T">The configuration value type.</typeparam>
519+ /// <param name="firstKeyPart">The first key part.</param>
520+ /// <param name="secondKeyPart">The second key part.</param>
521+ /// <param name="thirdKeyPart">The third key part.</param>
522+ /// <param name="defaultValue">The default value if the key is not set.</param>
523+ /// <returns>The configuration value, or the default.</returns>
524+ public virtual T GetValueOrDefault < T > ( string firstKeyPart , string secondKeyPart , string thirdKeyPart , T defaultValue )
525+ {
526+ return ValueOrDefault ( Get < T > ( firstKeyPart , secondKeyPart , thirdKeyPart ) , defaultValue ) ;
527+ }
528+
529+ /// <summary>
530+ /// Get a configuration value for the given key,
531+ /// or a value generated by <paramref name="defaultValueSelector" />
532+ /// if the key is not set.
533+ /// </summary>
534+ /// <typeparam name="T">The configuration value type.</typeparam>
535+ /// <param name="key">The key</param>
536+ /// <param name="defaultValueSelector">The selector used to generate a default value if the key is not set.</param>
537+ /// <returns>The configuration value, or a generated default.</returns>
538+ public virtual T GetValueOrDefault < T > ( string key , Func < T > defaultValueSelector )
539+ {
540+ return ValueOrDefault ( Get < T > ( key ) , defaultValueSelector ) ;
541+ }
542+
543+ /// <summary>
544+ /// Get a configuration value for the given key,
545+ /// or a value generated by <paramref name="defaultValueSelector" />
546+ /// if the key is not set.
547+ /// </summary>
548+ /// <typeparam name="T">The configuration value type.</typeparam>
549+ /// <param name="key">The key.</param>
550+ /// <param name="level">The configuration file into which the key should be searched for.</param>
551+ /// <param name="defaultValueSelector">The selector used to generate a default value if the key is not set.</param>
552+ /// <returns>The configuration value, or a generated default.</returns>
553+ public virtual T GetValueOrDefault < T > ( string key , ConfigurationLevel level , Func < T > defaultValueSelector )
554+ {
555+ return ValueOrDefault ( Get < T > ( key , level ) , defaultValueSelector ) ;
556+ }
557+
558+ /// <summary>
559+ /// Get a configuration value for the given key parts,
560+ /// or a value generated by <paramref name="defaultValueSelector" />
561+ /// if the key is not set.
562+ /// </summary>
563+ /// <typeparam name="T">The configuration value type.</typeparam>
564+ /// <param name="keyParts">The key parts.</param>
565+ /// <param name="defaultValueSelector">The selector used to generate a default value if the key is not set.</param>
566+ /// <returns>The configuration value, or a generated default.</returns>
567+ public virtual T GetValueOrDefault < T > ( string [ ] keyParts , Func < T > defaultValueSelector )
568+ {
569+ return ValueOrDefault ( Get < T > ( keyParts ) , defaultValueSelector ) ;
570+ }
571+
572+ /// <summary>
573+ /// Get a configuration value for the given key parts,
574+ /// or a value generated by <paramref name="defaultValueSelector" />
575+ /// if the key is not set.
576+ /// </summary>
577+ /// <typeparam name="T">The configuration value type.</typeparam>
578+ /// <param name="firstKeyPart">The first key part.</param>
579+ /// <param name="secondKeyPart">The second key part.</param>
580+ /// <param name="thirdKeyPart">The third key part.</param>
581+ /// <param name="defaultValueSelector">The selector used to generate a default value if the key is not set.</param>
582+ /// <returns>The configuration value, or a generated default.</returns>
583+ public virtual T GetValueOrDefault < T > ( string firstKeyPart , string secondKeyPart , string thirdKeyPart , Func < T > defaultValueSelector )
584+ {
585+ return ValueOrDefault ( Get < T > ( firstKeyPart , secondKeyPart , thirdKeyPart ) , defaultValueSelector ) ;
586+ }
587+
588+ private static T ValueOrDefault < T > ( ConfigurationEntry < T > value , T defaultValue )
589+ {
590+ return value == null ? defaultValue : value . Value ;
591+ }
592+
593+ private static T ValueOrDefault < T > ( ConfigurationEntry < T > value , Func < T > defaultValueSelector )
594+ {
595+ Ensure . ArgumentNotNull ( defaultValueSelector , "defaultValueSelector" ) ;
596+
597+ return value == null
598+ ? defaultValueSelector ( )
599+ : value . Value ;
600+ }
601+
369602 /// <summary>
370603 /// Set a configuration value for a key in the local configuration. Keys are in the form 'section.name'.
371604 /// <para>
0 commit comments