LCOV - code coverage report
Current view: top level - vs-net-core-template\CalculatorLibrary\CalculatorCli.cs - CalculatorCli.cs (source / functions) Hit Total Coverage
Test: Documentation Coverage Report Lines: 4 5 80.0 %
Date: Wed Sep 30 03:07:31 2026 Functions: 0 0 -
Legend: Lines: hit not hit

          Line data    Source code
       1           0 : namespace CalculatorLibrary {
       2             : 
       3             : /// <summary>
       4             : /// Parses and runs the calculator's command-line arguments.
       5             : /// </summary>
       6             : /// <remarks>
       7             : /// <para>
       8             : /// The parsing logic lives here, in the library, instead of in <c>CalculatorApp/Program.cs</c>, so
       9             : /// it can be unit tested directly (see <c>CalculatorLibrary.Tests/CalculatorCliTests.cs</c>) without
      10             : /// spawning the console application as a process. <c>Program.cs</c> only calls
      11             : /// <see cref="Run(string[])"/> or <see cref="RunDemo"/> and prints the result — it never reads from
      12             : /// the console, so it never blocks when the app is launched from a script, a build pipeline or CI.
      13             : /// </para>
      14             : /// <para>
      15             : /// When you rename this sample into your own project, replace this class with your own
      16             : /// argument-parsing entry point, keeping the same "parsing is a library function, not something the
      17             : /// console shell does inline" shape.
      18             : /// </para>
      19             : /// </remarks>
      20           1 : public static class CalculatorCli {
      21             :   /// <summary>The usage line shown when the arguments cannot be parsed.</summary>
      22           1 :   public const string UsageText = "Usage: CalculatorApp <add|subtract|multiply|divide> <a> <b>";
      23             : 
      24             :   /// <summary>
      25             :   /// Parses <paramref name="args"/> as <c>&lt;operation&gt; &lt;a&gt; &lt;b&gt;</c> and returns a
      26             :   /// line of text describing the result.
      27             :   /// </summary>
      28             :   /// <param name="args">
      29             :   /// Exactly three arguments: the operation name (<c>add</c>, <c>subtract</c>, <c>multiply</c> or
      30             :   /// <c>divide</c>, case-insensitive) followed by the two integer operands.
      31             :   /// </param>
      32             :   /// <returns>A human-readable line such as <c>"2 add 2 = 4"</c>.</returns>
      33             :   /// <exception cref="ArgumentException">
      34             :   /// Thrown when the argument count is wrong, an operand is not a valid 32-bit integer, or the
      35             :   /// operation name is not recognized. The message always ends with <see cref="UsageText"/>.
      36             :   /// </exception>
      37             :   /// <exception cref="DivideByZeroException">Thrown when dividing by zero.</exception>
      38           1 :   public static string Run(string[] args) {
      39             :     if (args.Length != 3) {
      40             :       throw new ArgumentException($"Expected 3 arguments (operation, a, b) but got {args.Length}. {UsageText}");
      41             :     }
      42             : 
      43             :     string operation = args[0].Trim().ToLowerInvariant();
      44             : 
      45             :     if (!int.TryParse(args[1], out int a)) {
      46             :       throw new ArgumentException($"'{args[1]}' is not a valid 32-bit integer for <a>. {UsageText}");
      47             :     }
      48             : 
      49             :     if (!int.TryParse(args[2], out int b)) {
      50             :       throw new ArgumentException($"'{args[2]}' is not a valid 32-bit integer for <b>. {UsageText}");
      51             :     }
      52             : 
      53             :     var calculator = new Calculator();
      54             : 
      55             :     int result = operation switch {
      56             :       "add" => calculator.Add(a, b),
      57             :         "subtract" => calculator.Subtract(a, b),
      58             :         "multiply" => calculator.Multiply(a, b),
      59             :         "divide" => calculator.Divide(a, b),
      60             :         _ => throw new ArgumentException($"Unknown operation '{args[0]}'. {UsageText}"),
      61             :     };
      62             : 
      63             :     return $"{a} {operation} {b} = {result}";
      64             :   }
      65             : 
      66             :   /// <summary>
      67             :   /// Runs a small built-in demonstration that exercises every operation once, used when the
      68             :   /// application is started with no arguments at all (so a first run always shows something,
      69             :   /// without waiting on keyboard input).
      70             :   /// </summary>
      71             :   /// <returns>The combined demo output, ending with <see cref="UsageText"/>.</returns>
      72           1 :   public static string RunDemo() {
      73             :     var calculator = new Calculator();
      74             :     var lines = new System.Text.StringBuilder();
      75             :     lines.AppendLine("Calculator Application Running (no arguments given, showing the built-in demo)...");
      76             :     lines.AppendLine($"2 add 2 = {calculator.Add(2, 2)}");
      77             :     lines.AppendLine($"2 multiply 2 = {calculator.Multiply(2, 2)}");
      78             :     lines.AppendLine($"2 subtract 2 = {calculator.Subtract(2, 2)}");
      79             :     lines.AppendLine($"2 divide 2 = {calculator.Divide(2, 2)}");
      80             :     lines.Append(UsageText);
      81             :     return lines.ToString();
      82             :   }
      83             : }
      84             : }

Generated by: LCOV version 1.15.alpha0w