17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/laser-cutter/cli/opt_parser.rb', line 17
def self.parse(args)
banner_text = "\#{('Laser-Cutter v'+ Laser::Cutter::VERSION).bold}\n\nUsage: laser-cutter [options] -o filename.pdf\n eg: laser-cutter -z 1x1.5x2/0.125 -O -o box.pdf\n EOF\n\n examples = <<-EOF\n\nExamples:\n 1. Create a box defined in inches, set kerf to 0.008\" and open PDF in preview right after:\n\n laser-cutter -z 3x2x2/0.125 -k 0.008 -O -o box.pdf\n\n 2. Create a box defined in millimeters, print verbose info, and set\n page size to A3, and layout to landscape, and stroke width to 1/2mm:\n\n laser-cutter -u mm -w70 -h20 -d50 -t4.3 -n5 -iA3 -l landscape -s0.5 -v -O -o box.pdf\n\n 3. List all possible page sizes in metric systems:\n\n laser-cutter -L -u mm\n\n 4. Create a box with provided dimensions, and save the config to a file\n for later use:\n\n laser-cutter -z 1.1x2.5x1.5/0.125/0.125 -p 0.1 -O -o box.pdf -W box-settings.json\n\n 5. Read settings from a previously saved file:\n\n laser-cutter -O -o box.pdf -R box-settings.json\n cat box-settings.json | laser-cutter -O -o box.pdf -R -\n\n EOF\n options = Hashie::Mash.new\n options.verbose = false\n options.units = 'in'\n\n opt_parser = OptionParser.new do |opts|\n opts.banner = banner_text.blue\n opts.separator \"Specific Options:\"\n opts.on(\"-w\", \"--width WIDTH\", \"Internal width of the box\") { |value| options.width = value }\n opts.on(\"-h\", \"--height HEIGHT\", \"Internal height of the box\") { |value| options.height = value }\n opts.on(\"-d\", \"--depth DEPTH\", \"Internal depth of the box\") { |value| options.depth= value }\n opts.on(\"-t\", \"--thickness THICKNESS\", \"Thickness of the box material\") { |value| options.thickness = value }\n opts.on(\"-n\", \"--notch NOTCH\", \"Optional notch length (aka \\\"tab width\\\"), guide only\") { |value| options.notch = value }\n opts.on(\"-k\", \"--kerf KERF\", \"Kerf - cut width (default is \#{Laser::Cutter::Configuration::UNIT_SPECIFIC_DEFAULTS['in'][:kerf]}in)\") { |value| options.kerf = value }\n opts.separator \"\"\n opts.on(\"-m\", \"--margin MARGIN\", \"Margins from the edge of the document\") { |value| options.margin = value }\n opts.on(\"-p\", \"--padding PADDING\", \"Space between the boxes on the page\") { |value| options.padding = value }\n opts.on(\"-s\", \"--stroke WIDTH\", \"Numeric stroke width of the line\") { |value| options.stroke = value }\n opts.on(\"-i\", \"--page_size LETTER\", \"Document page size, default is autofit the box.\") { |value| options.page_size = value }\n opts.on(\"-l\", \"--page_layout portrait\", \"Page layout, other option is 'landscape' \") { |value| options.page_layout = value }\n opts.separator \"\"\n opts.on(\"-O\", \"--open\", \"Open generated file with system viewer before exiting\") { |v| options.open = v }\n opts.on(\"-W\", \"--write CONFIG_FILE\", \"Save provided configuration to a file, use '-' for STDOUT\") { |v| options.write_file = v }\n opts.on(\"-R\", \"--read CONFIG_FILE\", \"Read configuration from a file, or use '-' for STDIN\") { |v| options.read_file = v }\n opts.separator \"\"\n opts.on(\"-L\", \"--list-all-page-sizes\", \"Print all available page sizes with dimensions and exit\") { |v| options.list_all_page_sizes = true }\n opts.on(\"-M\", \"--no-metadata\", \"Do not print box metadata on the PDF\") { |value| options.metadata = value }\n opts.on(\"-v\", \"--[no-]verbose\", \"Run verbosely\") { |v| options.verbose = v }\n opts.on(\"-B\", \"--inside-box\", \"Draw the inside boxes (helpful to verify kerfing)\") { |v| options.inside_box = v }\n opts.on(\"-D\", \"--debug\", \"Show full exception stack trace on error\") { |v| options.debug = v }\n opts.separator \"\"\n opts.on(\"--examples\", \"Show detailed usage examples\") { puts opts; puts examples.yellow; exit }\n opts.on(\"--help\", \"Show this message\") { puts opts; exit }\n opts.on(\"--version\", \"Show version\") { puts Laser::Cutter::VERSION; exit }\n opts.separator \"\"\n opts.separator \"Common Options:\"\n opts.on_tail(\"-o\", \"--file FILE\", \"Required output filename of the PDF\") { |value| options.file = value }\n opts.on_tail(\"-z\", \"--size WxHxD/T[/N]\",\n \"Combined internal dimensions: W = width, H = height,\\n\#{\" \" * 37}D = depth, T = thickness, and optional N = notch length\\n\\n\") do |size|\n options.size = size\n end\n opts.on_tail(\"-u\", \"--units UNITS\", \"Either 'in' for inches (default) or 'mm'\") { |value| options.units = value }\n end\n\n opt_parser.parse!(args)\n\n if options.read_file\n # these options are kept from the command line\n override_with = %w(debug verbose read_file)\n keep = options.reject{ |k,v| !override_with.include?(k)}\n Serializer.new(options).deserialize\n options.merge!(keep)\n end\n\n config = Laser::Cutter::Configuration.new(options.to_hash)\n if config.list_all_page_sizes\n puts PageManager.new(config.units).all_page_sizes\n exit 0\n end\n\n if options.verbose\n puts \"Starting with the following configuration:\"\n puts JSON.pretty_generate(config.to_hash).green\n end\n\n config.validate!\n\n if config.write_file\n Serializer.new(config).serialize\n end\n\n config\nrescue OptionParser::InvalidOption, OptionParser::MissingArgument, Laser::Cutter::MissingOption => e\n puts opt_parser.banner.blue\n puts_error(e)\n exit 1\nend\n"
|