Skip to main content
2021 Java Reflection Custom Annotations

ArgPar

Annotation-driven CLI argument parsing library for Java using reflection

ArgPar screenshot

Overview

ArgPar is a declarative CLI argument parsing library for Java that uses custom annotations to define command-line interfaces. Users annotate fields on plain Java classes with @ParsableArgument, @ParsableOption, or @ParsableFlag, and ArgPar uses reflection to introspect those fields, parse CLI arguments, convert string values to the target type, and populate the object at runtime. It supports positional arguments, named options, boolean flags, enum types, array types, custom types via the valueOf(String) convention, subcommand dispatch, and automatic help generation.

Highlights

  • Designed a declarative annotation-driven CLI parsing library (~548 lines) that eliminates all imperative parsing boilerplate
  • Built an extensible type system via reflection: the valueOf(String) convention allows any user-defined type to be parsable without modifying the library
  • Implemented polymorphic parse dispatch with consumed-index tracking enabling order-independent argument/option/flag parsing
  • Practical teaching tool used by students in the Compilers and Virtual Machines (PINS) course to control compiler phase execution