private static async Task Main()

in CommandLine/XblConfig/Program.cs [22:117]


        private static async Task<int> Main(string[] args)
        {
            VirtualTerminal.Enable();

            int exitCode = 0;
            DevAccount account = ToolAuthentication.LoadLastSignedInUser();
            if (account == null)
            {
                Console.Error.WriteLine("Didn't find dev signin info, please use \"XblDevAccount.exe signin\" to initiate.");
                return -1;
            }

            if (account.AccountSource != DevAccountSource.WindowsDevCenter)
            {
                Console.Error.WriteLine("You must sign in with a valid Windows Dev Center account.");
            }

            string invokedVerb = null;
            object opts = null;

            Console.WriteLine($"Using Dev account {account.Name} from {account.AccountSource}");
            try
            {
                // Find all of the subclasses which inherit from BaseOptions.
                Type[] argumentTypes = typeof(Program).GetNestedTypes(BindingFlags.NonPublic).Where(c => c.IsSubclassOf(typeof(BaseOptions))).ToArray();

                // Only assign the option and verb here, as the commandlineParser doesn't support async callback yet.
                Parser.Default.ParseArguments(args, argumentTypes)
                    .WithParsed(options =>
                    {
                        VerbAttribute verbAttribute = Attribute.GetCustomAttribute(options.GetType(), typeof(VerbAttribute)) as VerbAttribute;
                        invokedVerb = verbAttribute?.Name;
                        opts = options;
                    })
                    .WithNotParsed(err => exitCode = -1);

                if (opts != null)
                {
                    // Find property called AccountId. If it not set, then set it to the logged in user's account Id.
                    PropertyInfo accountIdProperty = opts.GetType().GetProperty("AccountId");
                    if (accountIdProperty != null)
                    {
                        Guid accountIdPropertyValue = (Guid)accountIdProperty.GetValue(opts);
                        if (accountIdPropertyValue == Guid.Empty)
                        {
                            accountIdProperty.SetValue(opts, new Guid(account.AccountId));
                        }
                    }

                    // Find the method which takes this class as an argument.
                    MethodInfo method = typeof(Program).GetMethods(BindingFlags.Static | BindingFlags.NonPublic).Where(c => c.GetParameters().FirstOrDefault()?.ParameterType == opts.GetType()).FirstOrDefault();
                    if (method == null)
                    {
                        // This should never happen, but just in case...
                        throw new InvalidOperationException($"Method with parameter {opts.GetType()} not found.");
                    }

                    Task<int> methodResult = (Task<int>)method.Invoke(null, new object[] { opts });
                    exitCode = await methodResult;
                }
            }
            catch (HttpRequestException ex)
            {
                Console.Error.WriteLine($"Error: XblConfig {invokedVerb} failed.");

                if (ex.Message.Contains(Convert.ToString((int)HttpStatusCode.Unauthorized)))
                {
                    Console.Error.WriteLine(
                        $"Unable to authorize the account with the XboxLive service, please contact your administrator.");
                }
                else if (ex.Message.Contains(Convert.ToString((int)HttpStatusCode.Forbidden)))
                {
                    Console.Error.WriteLine(
                        "Your account doesn't have access to perform the operation, please contact your administrator.");
                }
                else
                {
                    Console.WriteLine(ex.Message);
                }

                exitCode = -1;
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Error: unexpected error found.");
                Console.Error.WriteLine(ex.Message);
                exitCode = -1;
            }

            if (System.Diagnostics.Debugger.IsAttached)
            {
                Console.Read();
            }

            return exitCode;
        }