Skip to content

emulate beautysh --force-function-style #1064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
williamspatrick opened this issue Mar 1, 2024 · 3 comments
Open

emulate beautysh --force-function-style #1064

williamspatrick opened this issue Mar 1, 2024 · 3 comments

Comments

@williamspatrick
Copy link

Would it be possible to easily emulate the --force-function-style behavior of beautysh? That seems to be the only missing feature to use this as a replacement.

  --force-function-style FORCE_FUNCTION_STYLE, -s FORCE_FUNCTION_STYLE
                        Force a specific Bash function formatting. See below for more info.

...
Bash function styles that can be specified via --force-function-style are:
  fnpar: function keyword, open/closed parentheses, e.g.      function foo()
  fnonly: function keyword, no open/closed parentheses, e.g.  function foo
  paronly: no function keyword, open/closed parentheses, e.g. foo()

I usually set this to fnpar.

@mvdan
Copy link
Owner

mvdan commented Mar 3, 2024

shfmt supports all three of these forms, but it's true that it doesn't support enforcing any of the three forms consistently. I agree that being able to do that would be nice for Bash. It's worth pointing out that this is not a problem for POSIX Shell, where foo() is the only option.

That said, the bar for new formatting options is high - I really want to avoid a situation like https://linux.die.net/man/1/indent with dozens of formatting options. I'm not sure whether we can support this without a new option, because I suspect people have different preferences for Bash function declarations.

Perhaps the answer is to repurpose the existing --func-next-line in v4 so that it becomes a --func-style flag with a string, with options like:

  • paren - foo() {
  • paren-newl - foo()\n{
  • func - function foo {
  • func-newl - function foo\n{
  • func-paren - function foo() {
  • func-paren-newl - function foo()\n{

This "blows up" the option into a 2x3 matrix, but I think that's fine as I don't suspect it will need to grow any further.

@pauldraper
Copy link

POSIX Shell, where foo() is the only option

I really want to avoid a situation like https://linux.die.net/man/1/indent with dozens of formatting options

So which is it? :)

Opinionated or not.


The choice of brace+newlines is entirely optional for this request, beautysh does not include that option.


Is there any argument for not using the POSIX standard?

It's shorter and more compatible.

@mvdan
Copy link
Owner

mvdan commented Apr 5, 2025

I guess we could enforce using the POSIX foo() { form only, but I wonder if that would be too restrictive. When writing Bash, which is realistically what most people do these days, using e.g. function foo() { is perfectly fine as long as it's consistent.

Not everyone cares about POSIX compatibility, and for those who do, shfmt --posix is the better solution to restrict the syntax to POSIX.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants