diff --git a/spec/help_spec.lua b/spec/help_spec.lua index aadbabd..3c87d58 100644 --- a/spec/help_spec.lua +++ b/spec/help_spec.lua @@ -593,5 +593,30 @@ Options: --foo words, it should be broken down. -h, --help Show this help message and exit.]], parser:get_help()) end) + + it("autowraps description and epilog", function() + local parser = Parser "foo" + :help_max_width(80) + :description("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor " .. + "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation " .. + "ullamco laboris nisi ut aliquip ex ea commodo consequat.") + :epilog("Duis aute irure dolor in reprehenderit " .. + "in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat " .. + "non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.") + + assert.equal([[ +Usage: foo [-h] + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor +incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis +nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. + +Options: + -h, --help Show this help message and exit. + +Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu +fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in +culpa qui officia deserunt mollit anim id est laborum.]], parser:get_help()) + end) end) end) diff --git a/src/argparse.lua b/src/argparse.lua index 4643fc4..43f85ba 100644 --- a/src/argparse.lua +++ b/src/argparse.lua @@ -955,8 +955,16 @@ function Parser:get_help() local blocks = {self:get_usage()} + local help_max_width = self:_inherit_property("help_max_width") + if self._description then - table.insert(blocks, self._description) + local description = self._description + + if help_max_width then + description = table.concat(autowrap(split_lines(description), help_max_width), "\n") + end + + table.insert(blocks, description) end -- 1. Put groups containing arguments first, then other arguments. @@ -1005,7 +1013,13 @@ function Parser:get_help() end if self._epilog then - table.insert(blocks, self._epilog) + local epilog = self._epilog + + if help_max_width then + epilog = table.concat(autowrap(split_lines(epilog), help_max_width), "\n") + end + + table.insert(blocks, epilog) end return table.concat(blocks, "\n\n")