33namespace ShipMonk \ComposerDependencyAnalyser ;
44
55use Composer \Autoload \ClassLoader ;
6+ use Composer \InstalledVersions ;
7+ use OutOfBoundsException ;
68use ShipMonk \ComposerDependencyAnalyser \Config \Configuration ;
79use ShipMonk \ComposerDependencyAnalyser \Config \ErrorType ;
10+ use ShipMonk \ComposerDependencyAnalyser \Exception \AbortException ;
811use ShipMonk \ComposerDependencyAnalyser \Exception \InvalidCliException ;
912use ShipMonk \ComposerDependencyAnalyser \Exception \InvalidConfigException ;
1013use ShipMonk \ComposerDependencyAnalyser \Exception \InvalidPathException ;
1114use ShipMonk \ComposerDependencyAnalyser \Result \ConsoleFormatter ;
1215use ShipMonk \ComposerDependencyAnalyser \Result \JunitFormatter ;
1316use ShipMonk \ComposerDependencyAnalyser \Result \ResultFormatter ;
1417use Throwable ;
18+ use function class_exists ;
1519use function count ;
1620use function get_class ;
1721use function is_file ;
@@ -28,6 +32,7 @@ class Initializer
2832 vendor/bin/composer-dependency-analyser
2933
3034Options:
35+ --version Print analyser version.
3136 --help Print this help text and exit.
3237 --verbose Print more usage examples
3338 --show-all-usages Removes the limit of showing only few usages
@@ -220,6 +225,7 @@ public function initComposerClassLoaders(): array
220225
221226 /**
222227 * @param list<string> $argv
228+ * @throws AbortException
223229 * @throws InvalidCliException
224230 */
225231 public function initCliOptions (string $ cwd , array $ argv ): CliOptions
@@ -228,7 +234,12 @@ public function initCliOptions(string $cwd, array $argv): CliOptions
228234
229235 if ($ cliOptions ->help !== null ) {
230236 $ this ->stdOutPrinter ->printLine (self ::$ help );
231- throw new InvalidCliException ('' ); // just exit
237+ throw new AbortException ();
238+ }
239+
240+ if ($ cliOptions ->version !== null ) {
241+ $ this ->stdOutPrinter ->printLine ($ this ->deduceVersion ());
242+ throw new AbortException ();
232243 }
233244
234245 return $ cliOptions ;
@@ -256,4 +267,19 @@ public function initFormatter(CliOptions $options): ResultFormatter
256267 throw new InvalidConfigException ("Invalid format option provided, allowed are 'console' or 'junit'. " );
257268 }
258269
270+ private function deduceVersion (): string
271+ {
272+ try {
273+ /** @throws OutOfBoundsException */
274+ $ version = class_exists (InstalledVersions::class)
275+ ? InstalledVersions::getPrettyVersion ('shipmonk/composer-dependency-analyser ' )
276+ : 'unknown ' ;
277+
278+ } catch (OutOfBoundsException $ e ) {
279+ $ version = 'not found ' ;
280+ }
281+
282+ return "Version: $ version " ;
283+ }
284+
259285}
0 commit comments