Calculator Application with Library 2.1.2
A simple library for performing basic arithmetic operations with application
Loading...
Searching...
No Matches
Static Public Member Functions | Static Public Attributes | List of all members
CalculatorLibrary.CalculatorCli Class Reference

Parses and runs the calculator's command-line arguments. More...

Static Public Member Functions

static string Run (string[] args)
 Parses args as <operation> <a> <b> and returns a line of text describing the result.
 
static string RunDemo ()
 Runs a small built-in demonstration that exercises every operation once, used when the application is started with no arguments at all (so a first run always shows something, without waiting on keyboard input).
 

Static Public Attributes

const string UsageText = "Usage: CalculatorApp <add|subtract|multiply|divide> <a> <b>"
 The usage line shown when the arguments cannot be parsed.
 

Detailed Description

Parses and runs the calculator's command-line arguments.

The parsing logic lives here, in the library, instead of in CalculatorApp/Program.cs, so it can be unit tested directly (see CalculatorLibrary.Tests/CalculatorCliTests.cs) without spawning the console application as a process. Program.cs only calls Run(string[]) or RunDemo and prints the result — it never reads from the console, so it never blocks when the app is launched from a script, a build pipeline or CI.

When you rename this sample into your own project, replace this class with your own argument-parsing entry point, keeping the same "parsing is a library function, not something the console shell does inline" shape.

Definition at line 20 of file CalculatorCli.cs.

Member Function Documentation

◆ Run()

static string CalculatorLibrary.CalculatorCli.Run ( string[]  args)
static

Parses args as <operation> <a> <b> and returns a line of text describing the result.

Parameters
argsExactly three arguments: the operation name (add, subtract, multiply or divide, case-insensitive) followed by the two integer operands.
Returns
A human-readable line such as "2 add 2 = 4".
Exceptions
ArgumentExceptionThrown when the argument count is wrong, an operand is not a valid 32-bit integer, or the operation name is not recognized. The message always ends with UsageText.
DivideByZeroExceptionThrown when dividing by zero.

Definition at line 38 of file CalculatorCli.cs.

38 {
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 }

◆ RunDemo()

static string CalculatorLibrary.CalculatorCli.RunDemo ( )
static

Runs a small built-in demonstration that exercises every operation once, used when the application is started with no arguments at all (so a first run always shows something, without waiting on keyboard input).

Returns
The combined demo output, ending with UsageText.

Definition at line 72 of file CalculatorCli.cs.

72 {
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 }
const string UsageText
The usage line shown when the arguments cannot be parsed.

References CalculatorLibrary.CalculatorCli.UsageText.

Member Data Documentation

◆ UsageText

const string CalculatorLibrary.CalculatorCli.UsageText = "Usage: CalculatorApp <add|subtract|multiply|divide> <a> <b>"
static

The usage line shown when the arguments cannot be parsed.

Definition at line 22 of file CalculatorCli.cs.

Referenced by CalculatorLibrary.CalculatorCli.RunDemo().


The documentation for this class was generated from the following file: