//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;

#if !WASM
namespace CommonHelpers {
#else
namespace CommonHelpers.WASM {
#endif

//**************************************************************************************************
/// <summary>
/// </summary>
public static partial class HFlagParser {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// <exception cref="HParseValueException" />
public static bool Parse(string src)
{
   return ParseEx(src,null);
}
/// <exception cref="HParseValueException" />
public static bool Parse(string src,bool def)
{
   return ParseEx(src,def);
}

//==================================================================================================

public static bool TryParse(string src,out string dst)
{
   return TryParse(src,out dst);
}
public static bool TryParse<T>(string src,bool def,out bool dst)
{
   HParseValueException e;
   return TryParse(src,def,out dst,out e);
}

//--------------------------------------------------------------------------------------------------

public static bool TryParse(string src,out bool dst,out HParseValueException e)
{
   try
      { dst=Parse(src);}
   catch (HParseValueException x)
      { dst=false; e=x; return false; }
   e=null; return true;
}
public static bool TryParse(string src,bool def,out bool dst,out HParseValueException e)
{
   try
      { dst=Parse(src,def); }
   catch (HParseValueException x)
      { dst=false; e=x; return false; }
   e=null; return true;
}

//==================================================================================================

/// <exception cref="HParseValueException" />
public static bool ParseEx(string src,bool? def)
{
   HArgChecking.VerifyNotNull(src);

   src = src.Trim();

   switch ( src )
   {
      case "":
         if ( def.HasValue )
            return def.Value;
         else
            throw new HParseValueException( "Empty string for flag value." );
      case "0": return false;
      case "1": return true;
      default:
         throw new HParseValueException( "Incorrect flag value (not 1 or 0)." );
   }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HFlagParser
//**************************************************************************************************

} // CommonHelpers (.WASM)
