/// <summary> /// Prints program usage message. /// </summary> private static void ShowUsage(ExampleRunner runner) { string exeName = Path.GetFileName(Assembly.GetExecutingAssembly().FullName); Console.WriteLine("Runs Google Ads API code examples"); Console.WriteLine("Usage : {0} examplename [flags]\n", exeName); Console.WriteLine($"Supported examples: \n"); runner.PrintAvailableExamples(); Console.WriteLine("Press [Enter] to continue"); Console.ReadLine(); }
/// <summary> /// Runs the examples from command line arguments. /// </summary> /// <param name="args">The command line arguments.</param> /// <returns>The application's exit code. The valid return codes are: /// /// <list type="bullet"> /// <item><description>0. The code example ran successfully.</description></item> /// <item><description>1. The code example threw an exception and did not complete /// successfully.</description></item> /// <item><description>2. The application was invoked with an incorrect command line /// argument.</description></item> /// </list> /// </returns> private static int RunExamplesFromCommandLineArguments(string[] args) { ExampleRunner runner = new ExampleRunner(); runner.LoadCodeExamples(Assembly.GetExecutingAssembly()); if (args.Length == 0) { // Bad command line parameter. ShowUsage(runner); return(2); } string exampleName = args[0]; try { runner.Run(exampleName, args.Skip(1)); return(0); } catch (Exception e) when(e is KeyNotFoundException) { // First arg is not a valid example name. Console.WriteLine("First arg is not a valid example name"); ShowUsage(runner); return(2); } catch (Exception e) when(e is ArgumentException) { // Args passed to runner are invalid Console.WriteLine(e.Message); Console.WriteLine(ExampleBase.GetUsage(runner.GetCodeExampleType(exampleName))); return(2); } catch (Exception e) { // Indicates a failure due to an unhandled exception. Console.WriteLine(e.Message); return(1); } }
/// <summary> /// Runs the examples from command line arguments. /// </summary> /// <param name="args">The command line arguments.</param> /// <returns>The application's exit code. The valid return codes are: /// /// <list type="bullet"> /// <item><description>0. The code example ran successfully.</description></item> /// <item><description>1. The code example threw an exception and did not complete /// successfully.</description></item> /// <item><description>2. The application was invoked with an incorrect command line /// argument.</description></item> /// </list> /// </returns> private static int RunExamplesFromCommandLineArguments(string[] args) { ExampleRunner runner = new ExampleRunner(); runner.LoadCodeExamples(Assembly.GetExecutingAssembly()); if (args.Length == 0) { // Bad command line parameter. ShowUsage(runner); return(2); } GoogleAdsClient session = new GoogleAdsClient(); string exampleName = args[0]; try { // Optional: Turn on profiling to see how long an API call takes. // runner.RunWithProfiling(exampleName, session, args); runner.Run(exampleName, session, args.Skip(1)); return(0); } catch (TargetInvocationException) { // Indicates a failure due to an unhandled exception. return(1); } catch (Exception e) when(e is KeyNotFoundException || e is ArgumentException || e is TargetParameterCountException) { // Bad command line parameter. // Note: There are a couple more exceptions that the runner may throw, but all // those indicate a failure with the runner implementation than a code example // failure. ShowUsage(runner); return(2); } }
/// <summary> /// Runs the examples from command line arguments. /// </summary> /// <param name="args">The command line arguments.</param> private static void RunExamplesFromCommandLineArguments(string[] args) { ExampleRunner runner = new ExampleRunner(); runner.LoadCodeExamples(Assembly.GetExecutingAssembly()); if (args.Length == 0) { ShowUsage(runner); return; } GoogleAdsClient session = new GoogleAdsClient(); string exampleName = args[0]; try { runner.Run(exampleName, session, args.Skip(1)); } catch (KeyNotFoundException) { ShowUsage(runner); } }