Skip to content

Commit

Permalink
Merge pull request #374 from bkeepers/help
Browse files Browse the repository at this point in the history
Improvements to dotenv CLI
  • Loading branch information
Andrew Nordman authored Feb 22, 2019
2 parents 4168b83 + 25908eb commit 56516b1
Showing 1 changed file with 42 additions and 11 deletions.
53 changes: 42 additions & 11 deletions lib/dotenv/cli.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require "dotenv"
require "dotenv/version"
require "optparse"

module Dotenv
# The CLI is a class responsible of handling all the command line interface
Expand All @@ -11,26 +13,55 @@ def initialize(argv = [])
end

def run
filenames = parse_filenames || []
parse_argv!(@argv)

begin
Dotenv.load!(*filenames)
Dotenv.load!(*@filenames)
rescue Errno::ENOENT => e
abort e.message
else
exec(*argv) unless argv.empty?
exec(*@argv) unless @argv.empty?
end
end

private

def parse_filenames
pos = argv.index("-f")
return nil unless pos
# drop the -f
argv.delete_at pos
# parse one or more comma-separated .env files
require "csv"
CSV.parse_line argv.delete_at(pos)
def parse_argv!(argv)
@filenames = []

OptionParser.new do |parser|
parser.banner = "Usage: dotenv [options]"
parser.separator ""
add_options(parser)
end.parse!(argv)

@filenames
end

def add_options(parser)
add_files_option(parser)
add_help_option(parser)
add_version_option(parser)
end

def add_files_option(parser)
parser.on("-f FILES", Array, "List of env files to parse") do |list|
@filenames = list
end
end

def add_help_option(parser)
parser.on("-h", "--help", "Display help") do
puts parser
exit
end
end

def add_version_option(parser)
parser.on("-v", "--version", "Show version") do
puts "dotenv #{Dotenv::VERSION}"
exit
end
end
end
end

0 comments on commit 56516b1

Please sign in to comment.