Hanami provides a command line interface with helpful commands for generating a new application, starting a console, starting a development server, displaying routes and more.

Gem commands

After an initial install via gem install hanami, hanami offers two commands:

  1. $ hanami --help
  2. Commands:
  3. hanami new APP # Generate a new Hanami app
  4. hanami version # Hanami version

hanami new

Generates a Hanami application with the given APP name, in a new directory from the current location.

  1. $ hanami new bookshelf # generates a new Bookshelf application in ./bookshelf
  2. $ hanami new my_app # generates a new MyApp application in ./my_app

hanami version

Displays the Hanami version of the currently installed gem.

  1. $ hanami version
  2. v2.0.0

Project commands

When executed from within a hanami project, hanami offers a different set of commands.

These commands can be listed using the --help flag.

  1. $ bundle exec hanami --help
  2. Commands:
  3. hanami console # Start app console (REPL)
  4. hanami generate [SUBCOMMAND]
  5. hanami middleware # Print app Rack middleware stack
  6. hanami routes # Print app routes
  7. hanami server # Start Hanami app server
  8. hanami version # Print Hanami app version

hanami console

Starts the Hanami console.

  1. $ bundle exec hanami console
  2. bookshelf[development]>

This command accepts an engine argument that can start the console using IRB or Pry.

  1. $ bundle exec hanami console --engine=irb # (the default)
  2. $ bundle exec hanami console --engine=pry

hanami generate

Hanami 2.0 provides two generators, one for actions and one for slices.

  1. $ bundle exec hanami generate --help
  2. Commands:
  3. hanami generate action NAME
  4. hanami generate slice NAME

These can be invoked as follows:

  1. $ hanami generate action books.show
  2. $ hanami generate slice api

See the actions and slices guides for example usage.

hanami middleware

Displays the Rack middleware stack as currently configured.

  1. $ bundle exec hanami middleware
  2. / Dry::Monitor::Rack::Middleware (instance)
  3. / Rack::Session::Cookie
  4. / Hanami::Middleware::BodyParser

This command accepts a --with-arguments option that will include initialization arguments:

  1. $ bundle exec hanami middleware --with-arguments
  2. / Dry::Monitor::Rack::Middleware (instance) args: []
  3. / Rack::Session::Cookie args: [{:key=>"my_app.session", :secret=>"secret", :expire_after=>31536000}]
  4. / Hanami::Middleware::BodyParser args: [:json]

hanami routes

Displays your application’s routes.

  1. $ bundle exec hanami routes
  2. GET / home.index as :root
  3. GET /books books.index
  4. GET /books/:id books.show
  5. POST /books books.create

By default, routes are displayed in “human friendly” format. Routes can be inspected in csv format via the format option:

  1. $ bundle exec hanami routes --format=csv
  2. METHOD,PATH,TO,AS,CONSTRAINTS
  3. GET,/,home.index,:root,""
  4. GET,/books,books.index,"",""
  5. GET,/books/:id,books.show,"",""
  6. POST,/books,books.create,"",""

hanami server

Launches hanami’s development server for developing against your application locally.

  1. $ bundle exec hanami server
  2. 14:33:28 - INFO - Using Guardfile at bookshelf/Guardfile.
  3. 14:33:28 - INFO - Puma starting on port 2300 in development environment.
  4. 14:33:28 - INFO - Guard is now watching at 'bookshelf'
  5. [43884] Puma starting in cluster mode...
  6. [43884] * Puma version: 6.0.0 (ruby 3.1.0-p0) ("Sunflower")
  7. [43884] * Min threads: 5
  8. [43884] * Max threads: 5
  9. [43884] * Environment: development
  10. [43884] * Master PID: 43884
  11. [43884] * Workers: 2
  12. [43884] * Restarts: (✔) hot (✖) phased
  13. [43884] * Preloading application
  14. [43884] * Listening on http://0.0.0.0:2300
  15. [43884] Use Ctrl-C to stop

This server is for local development only. In production, use the following to start your application serving web requests with Puma:

  1. bundle exec puma -C config/puma.rb

hanami version

Prints the version of the installed hanami gem.

  1. $ bundle exec hanami version
  2. v2.0.0